MATLAB Answers

How do i declare an array for cell in Mex?

6 views (last 30 days)
Hariprasad on 31 Oct 2014
Edited: Hariprasad on 6 Nov 2014
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


Sign in to comment.

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.
As James pointed out (see comment below), no memory has been allocated to the dims array. So have replaced
mwSize* dims
mwSize dims[3];


Show 7 older comments
Geoff Hayes
Geoff Hayes on 5 Nov 2014
You will need to attach your code (use the paperclip button) so that we can see what you have attempted.
Geoff Hayes
Geoff Hayes on 5 Nov 2014
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);
// etc.
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.

Sign in to answer this question.