Linux Matlab - is it possible to determine whether a directory is a link?
Show older comments
I wrote a simple Matlab script using "dir" to recursively follow directories in a linux environment. The only thing is I don't want to follow linked directories. Does Matlab have a way of figuring out which directories are linked directories? On my Linux window, linked dirs show up as
/dir_x -> /home/some_directory_path/some_other_path/
rather than just /dir_x
Accepted Answer
More Answers (1)
Jan
on 8 Jul 2011
I assume you need a Mex function, which calls stat and uses the non-Posix macro S_ISLNK. See: http://www.mitchr.me/SS/exampleCode/AUPG/fileData.c.html But it is not trivial to consider Unicode file names, see: Answers: Matlab string to wchar under linux and CSSM: 301249.
I cannot test this under linux - perhaps you need further headers:
#include "mex.h"
#include <sys\stat.h>
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
char *FileName;
struct stat S;
// Check number and type of arguments:
if (nrhs != 1) {
mexErrMsgTxt("*** FileIsLink[mex]: 1 input required.");
}
if (nlhs > 1) {
mexErrMsgTxt("*** FileIsLink[mex]: 1 output allowed.");
}
// Type of input arguments:
if (!mxIsChar(prhs[0])) {
mexErrMsgTxt("*** FileIsLink[mex]: 1st input must be the file name.");
}
// Obtain FileName:
if ((FileName = mxArrayToString(prhs[0])) == NULL) {
mexErrMsgTxt("*** FileIsLink[mex]: "
"Cannot convert FileName to C-string.");
}
// Get the status and create output:
if (stat(FileName, &S) == 0) {
plhs[0] = mxCreateLogicalScalar((mxLogical) S_ISLNK(s.st_mode));
} else { // File not found:
plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
}
mxFree(FileName);
return;
}
Categories
Find more on Search Path in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!