-
Notifications
You must be signed in to change notification settings - Fork 0
/
mex_whofile.c
60 lines (48 loc) · 1.36 KB
/
mex_whofile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include "mex.h"
#include "mat.h"
#include "matrix.h"
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
char *str;
/*(void) plhs; /* unused parameter */
MATFile *pmat;
int ndir;
const char **dir;
int i;
/* Check for proper number of input and output arguments */
if (nrhs != 1) {
mexErrMsgTxt("One input argument required.");
}
if(nlhs > 1){
mexErrMsgTxt("Too many output arguments.");
}
if (!(mxIsChar(prhs[0]))){
mexErrMsgTxt("Input must be of type string.\n.");
}
/* str is the name of the file which is checked */
str = mxArrayToString(prhs[0]);
/* Open the File */
pmat = matOpen(str, "r");
if (pmat == NULL) {
printf("Error opening file %s\n", str);
return;
}
/* Get the variable names inside the file */
dir = (const char **)matGetDir(pmat, &ndir);
if (dir == NULL) {
mexPrintf("Error reading directory of file %s\n", str);
return;
}
/* Create output variable */
plhs[0] = mxCreateCellMatrix((mwSize)ndir, 1);
for(i = 0; i < ndir; i++)
mxSetCell(plhs[0], i, mxCreateString(dir[i]));
mxFree(dir);
/* Close the file */
if (matClose(pmat) != 0) {
printf("Error closing file %s\n", str);
return;
}
mxFree(str);
}