How do i declare an array for cell in Mex?
Mostrar comentarios más antiguos
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
Respuestas (1)
Geoff Hayes
el 31 de Oct. de 2014
Editada: Geoff Hayes
el 3 de Nov. de 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 comentarios
Hariprasad
el 3 de Nov. de 2014
Geoff Hayes
el 3 de Nov. de 2014
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.
James Tursa
el 3 de Nov. de 2014
@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.
Geoff Hayes
el 3 de Nov. de 2014
@James - you're right, that was an oversight on my part. I will update that portion of the answer. Thanks!
Hariprasad
el 4 de Nov. de 2014
Geoff Hayes
el 4 de Nov. de 2014
Glad to have been able to help!
Hariprasad
el 5 de Nov. de 2014
Editada: Hariprasad
el 5 de Nov. de 2014
Geoff Hayes
el 5 de Nov. de 2014
You will need to attach your code (use the paperclip button) so that we can see what you have attempted.
Geoff Hayes
el 5 de Nov. de 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);
sprintf(tmppath,"%s%s",srcpath,myString);
// etc.
Hariprasad
el 6 de Nov. de 2014
Editada: Hariprasad
el 6 de Nov. de 2014
Categorías
Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!