How do i declare an array for cell in Mex?

Edited my original post.
I'm loading a mat file which has data as cell array(sized 1000x6). I have reached till the stage where I load the mat file but I want to check if the data from mat file was loaded successfully. I have attached the code here after making the corrections as suggested. I have compiled the code without error but probably memory leak issue has come up because when I run the code, Matlab crashes.
The cell array in the Mat file is a collection of strings and numbers like:-
Filename TotalData Rows Columns Date_created All_Ids
'Data_1' 1000 1000 16 '10-Jul-2013' 1000x1 double
'Data_2' 1000 1000 16 '10-Jul-2013' 1000x1 double
'Data_4' 1000 1000 16 '10-Jul-2013' 1000x1 double

Answers (1)

Geoff Hayes
Geoff Hayes on 31 Oct 2014
Edited: Geoff Hayes on 3 Nov 2014
Hariprasad - it would be easier to point out the problems if you had included all of your code, because it is unclear what line 73 is given that you haven't pasted 73+ lines of code in your question. In the future, please just attach your code to your question using the paperclip button. Given that this is a c file, you would have to change the extension to txt or m just to allow you to attach it.
A couple of problem is with how you try to create the cell arrays. Look at the first line
Dataset_info = mxCreateCellArray(ndims,dims);
Your are passing in ndims and dims, neither of which have been initialized at this point, and they are of the wrong data type. From the documentation for mxCreateCellArray, its function signature is
mxArray *mxCreateCellArray(mwSize ndim, const mwSize *dims);
This means that ndim is of type mwSize and dims is an array of type mwSize. They would need to be declared as
mwSize ndims;
mwSize dims[3];
and defined as (for example)
ndims = 3;
dims[0] = 3;
dims[1] = 2;
dims[2] = 1;
So what, in your code or from your file, should determine the number of dimension (and their sizes) for the Dataset_info and Dataset_OP arrays?
Start with fixing this problem, and then we'll go from there.
EDIT
As James pointed out (see comment below), no memory has been allocated to the dims array. So have replaced
mwSize* dims
with
mwSize dims[3];

10 Comments

I'm extremely sorry Sir, I had modified the codes just after I got the errors hence the lines don't look right. Basically what I'm trying here is to load a mat file which is a Cell array and I want to know the dimensions of this loaded Cell array from mat file.
Try using the below code (attached as text file) that can be used to determine the dimensions of the cell array
#include "mex.h"
#include "mat.h"
#include <string.h>
void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
MATFile* pMatFile = 0;
const char* pMatFileName = "cellData.mat";
const char* pMatVarName = 0;
mxArray* pMatVariable = 0;
mwSize numDims = 0;
size_t n = 0;
size_t m = 0;
// if user has passed in a string, use that for the mat file
if (nrhs==1)
{
if (mxIsChar(prhs[0]))
{
pMatFileName = mxArrayToString(prhs[0]);
}
}
// open the mat file
printf("Opening file %s...\n",pMatFileName);
pMatFile = matOpen(pMatFileName, "r");
if (pMatFile == NULL)
{
printf("Error opening file %s\n", pMatFileName);
}
else
{
// assume one variable in file, so get it
pMatVariable = matGetNextVariableInfo(pMatFile, &pMatVarName);
if (pMatVariable == 0)
{
printf("Error reading in next variable!\n");
}
else
{
printf("Reading in variable %s...\n",pMatVarName);
// get dimensions of array
numDims = mxGetNumberOfDimensions(pMatVariable);
printf("Number of dimension in %s is %d...\n",
pMatVarName,numDims);
if (numDims==2)
{
// assuming 2D, so get m and n
m = mxGetM(pMatVariable);
n = mxGetN(pMatVariable);
printf("%s is a %dx%d array...\n",pMatVarName,m,n);
}
// destroy array
mxDestroyArray(pMatVariable);
}
// close the mat file
if (matClose(pMatFile) != 0)
{
printf("Error closing file %s\n",pMatFileName);
}
}
}
Save the code to a file named (for example) myCellMatReader.c and build as
mex myCellMatReader.c
Create a (test) cell array as
cellData = {'Data_1' 1000 1000 16 '10-Jul-2013' randi(255,1000,1) ;
'Data_3' 1000 1000 16 '10-Jul-2013' randi(255,1000,1) ;
'Data_4' 1000 1000 16 '10-Jul-2013' randi(255,1000,1)};
and save it to file as
save('cellData.mat','cellData')
Now run the code as
myCellMatReader('cellData.mat')
and observe
Opening file cellData.mat...
Reading in variable cellData...
Number of dimension in cellData is 2...
cellData is a 3x6 array...
The above code was compiled with Clang on OS X 10.8.5, MATLAB R2014a.
@Geoff: In your Answer you have this line, but no associated allocation:
mwSize *dims;
I think (from your words) you meant this:
mwSize dims[3];
As written, your code would not work unless you add code to allocate dims.
@James - you're right, that was an oversight on my part. I will update that portion of the answer. Thanks!
Thank you @Geoff and @James. I tested myCellMatReader.c and I understood how ndims and dims are to be used. I suppose we can initialize dims[3] even though we have only 2D array. Further, in my code I have to use the first column data like 'Data_1' which are names of mat files to load to actually start the main process.
Quite a complex thing this Mex for me so far. I have just started my code and I have so many corrections. I am sure I will have to ask for some more help when I start continuing on my Code. Thanks again @Geoff and @James again.
Glad to have been able to help!
Hariprasad
Hariprasad on 5 Nov 2014
Edited: Hariprasad on 5 Nov 2014
Hello again!! Now I'm trying to load the mat file named 'Data_1' and fetch data in the file. I compiled the code what I have written, without errors but Matlab crashes when I run it. Pondering over it since yesterday!! Changed the code so many times, but in vein!! I know something is wrong in the code but cannot figure out what. Sorry, code attached!!
You will need to attach your code (use the paperclip button) so that we can see what you have attempted.
I suspect the problem is with the following lines (though without knowing what is in your mat file or what parameters you are passing the function, I could be wrong)
DataSetInfo_ptr = matGetNextVariableInfo(DsetmatFile,&DataSetVarName);
No_Files = mxGetM(DataSetInfo_ptr);
which gives you information about your variable in your mat file, which is presumably a cell array with No_Files rows.
You then get this variable with
DataSet_ptr = matGetVariable(DsetmatFile,DataSetVarName);
where DataSet_ptr is a pointer to an mxArray. A few lines later, the following happens
Dataset_info = mxGetPr(DataSet_ptr);
where Dataset_info is a pointer to a double. This variable is next used at the line
printf("Concatenating %s and %s\n",srcpath,Dataset_info[i]);
where you almost treat it as if it were a string. Is that the case? What can you say about your input data, and what are you attempting to do here?
If you think that the first element in your cell array is a string, then you have to do something like
cellElement = mxGetCell(DataSet_ptr,0);
myString = mxArrayToString(cellElement);
where the above two local variables have been declared as
mxArray *cellElement = 0;
char *myString;
Then you can do the concatenation as
printf("Concatenating %s and %s\n",srcpath,myString);
sprintf(tmppath,"%s%s",srcpath,myString);
// etc.
Hariprasad
Hariprasad on 6 Nov 2014
Edited: Hariprasad on 6 Nov 2014
Oh yes, the mat file first loaded( DsetmatFile ) has only one variable. So I hope the matGetVariableInfo would give the name of the data variable stored in the mat file.
Yes sir. The first value is a string in the cell array. The values are ' Data_1 ', ' Data_2 ',etc which are the mat file names in the first column of the DsetmatFile opened. These mat files would contain a double array of about 1000xN values each. I tried the mxGetCell and was able to load the correct mat file.
Thank you!!

Sign in to comment.

Categories

Asked:

on 31 Oct 2014

Edited:

on 6 Nov 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!