How to access matrix in matlab structure field from mex function?

I am trying to figure out how to access a matrix that is stored in a field in a matlab structure from a mex function.

This is an awfully long time ... Let me explain:

I have a matlab structure that has been defined as follows:

matrixStruct = struct('matrix', {4, 4, 4; 5, 5, 5; 6, 6 ,6}) 

I have a mex function in which I would like to get a pointer to the first element in the matrix (matrix [0] [0], in c terms), but I could not figure out how to do this.

I tried the following:

 /* Pointer to the first element in the matrix (supposedly)... */ double *ptr = mxGetPr(mxGetField(prhs[0], 0, "matrix"); /* Incrementing the pointer to access all values in the matrix */ for(i = 0; i < 3; i++){ printf("%f\n", *(ptr + (i * 3))); printf("%f\n", *(ptr + 1 + (i * 3))); printf("%f\n", *(ptr + 2 + (i * 3))); } 

As a result of this, the following occurs:

 4.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 

I also tried options for the following, thinking that maybe it was something warlike with nested function calls, but to no avail:

 /* Pointer to the first location of the mxArray */ mxArray *fieldValuePtr = mxGetField(prhs[0], 0, "matrix"); /* Get the double pointer to the first location in the matrix */ double *ptr = mxGetPr(fieldValuePtr); /* Same for loop code here as written above */ 

Does anyone have an idea on how I can achieve what I'm trying to do, or what I can do wrong?

Thanks!

Edit: according to yuk's comment, I tried to perform similar operations on a structure that has a field called an array, which is a one-dimensional doubling array.

The structure containing the array is defined as follows:

 arrayStruct = struct('array', {4.44, 5.55, 6.66}) 

I tried the following in arrayStruct from the mex function:

 mptr = mxGetPr(mxGetField(prhs[0], 0, "array")); printf("%f\n", *(mptr)); printf("%f\n", *(mptr + 1)); printf("%f\n", *(mptr + 2)); 

... but the result followed what was printed earlier:

 4.440000 0.000000 0.000000 
+6
struct matlab mex
source share
3 answers

You are trying to access a variable, which is an array of cells in MATLAB. Are you sure the data is being saved? What happens if you put a double array in a structure?

 matrixStruct = struct('matrix', [4, 4, 4; 5, 5, 5; 6, 6 ,6]) 

I think the problem is how MATLAB stores data in an array of cells. Try running this:

 double1 = 1; double2 = 1:2; cellempty = {[]}; celldouble1 = {1}; celldouble2 = {1:2}; cell2doubles = {1,2}; whos 

Exit:

 Name Size Bytes Class Attributes cell2doubles 1x2 136 cell celldouble1 1x1 68 cell celldouble2 1x1 76 cell cellempty 1x1 60 cell double1 1x1 8 double double2 1x2 16 double 

You can see that each element of the array of cells occupies up to 60 bytes in the size of numbers.

+1
source share

struct('field', {abc}) is a special form of the struct constructor that creates an array of structures that is the same size as an array of cells, placing each cell element in the field field of the corresponding structure element. That is, it is:

 s = struct('field', {abc}); 

gives the same result:

 s(1).field = a; s(2).field = b; s(3).field = c; 

The solution to your problem is to use square brackets to create a regular (non-element) array, for example:

 matrixStruct = struct('matrix', [4, 4, 4; 5, 5, 5; 6, 6 ,6]); 
+5
source share

I went through this: I have a structure with a field that is a matrix. In C ++, the corresponding structure is double** , for example. An attempt to access a field using engGetVariable(engine,MyStruct.theField) fails. I use a temporary variable to store MyStruct.theField , and then I use engGetVariable(engine, tempVar) , and the code to get the matrix field from the structure looks like this:

 // Fetch struct field using a temp variable std::string tempName = std::string(field_name) + "_temp"; std::string fetchField = tempName + " = " + std::string(struct_name) + "." + std::string(field_name) + "; "; matlabExecute(ep, fetchField); mxArray *matlabArray = engGetVariable(ep, tempName.c_str()); // Get variable elements const int count = mxGetNumberOfElements(matlabArray); T *data = (T*) mxGetData(matlabArray); for (int i = 0; i < count; i++) vector[i] = _isnan(data[i]) ? (T) (int) -9999 : (T) data[i]; // Clear temp variable std::string clearTempVar = "clear " + tempName + "; "; matlabExecute(ep, clearTempVar); // Destroy mx object mxDestroyArray(matlabArray); 

It is very similar to set the matrix field to struct . I did it

 // Create temp variable mxArray* array = convertVectorToMxArray(mat, nb_rows, nb_cols); const std::string temp_name = array_name + "_temp"; int ret = engPutVariable(ep, temp_name.c_str(), array); // Set variable to struct field const std::string cmd = std::string(array_name + " = " + temp_name + "; "); matlabExecute(ep, cmd); // Delete array mxDestroyArray(array); 
0
source share

All Articles