Updating variables in workspace from MEX functions

6 views (last 30 days)
In my current mex function, i need to update a couple of variables in the workspace(from where my function is called) from my mex function, for which i am currently trying to update one at a time...my current piece of code, which trying to do, looks like this:
mxArray *a_out_m;
int success;
static double *temp_ptr;
static double temp_val;
a_out_m = mexGetVariable("caller", "input_var");
temp_val = *mxGetPr(a_out_m);
temp_val = temp_val + (temp_val*10);
  2 Comments
jchaliss
jchaliss on 17 Jul 2013
The input and output class is similar to the ones given below
%OUTPUT%
classdef OUTPUT_T
properties
output_1 = 0;
output_2 = 0;
end
end
%INPUT%
classdef INPUT_T
properties
input_1 = 10;
input_2 = 20;
input_3 = [];
end
end
classdef SUB_T < handle
properties
SUB_1 = 0;
SUB_2 = 0;
SUB_3 = 0;
end
end
the purpose of the mex function is to interface with an existing c/c++ function whose input is to be calculated via matlab and plot the result in matlab. So, my basic idea was that, within the mexfunction i update a local structure, after updating it from the workspace variable, and pass is to the c/c++ function. Similarly, the output workspace variable is updated from a structure returned/updated from the c/c++ function. Here the input_3 is assigned as an element of type of another class 'SUB_T'.
James Tursa
James Tursa on 17 Jul 2013
So is this what you would like to do?
1) Pass an INPUT_T object to the mex function (for purposes of discussion suppose this variable is named V in the caller workspace)
2) Have the mex function update the input_3 property of V
3) Pass this updated object V to another c/c++ function AND also have that update reflected in the original caller workspace variable V
4) Pass some output from the c/c++ function back to MATLAB to plot.
You can use mxGetProperty and mxSetProperty to do this in a mex function, but it will only work reliably if V is not shared (i.e., you never did something like W = V at the caller workspace level).

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 16 Jul 2013
Edited: James Tursa on 16 Jul 2013
What you are doing wrong is mixing C/C++ native memory with MATLAB memory. If you do this MATLAB will crash, as you have found out. The culprit is this line:
mxSetPr(a_out_m,temp_ptr);
The value contained in temp_ptr came from this line:
temp_ptr = &temp_val;
temp_val is just a regular double in the C/C++ program ... it is NOT something that is allocated from the MATLAB Memory Manager. Thus, you can't use its address in a mxSetPr call or you will crash MATLAB.
If you are just trying to update a scalar double workspace variable on the fly, try this:
a_out_m = mexGetVariable("caller", "input_var"); // Get a deep copy of input_var
temp_ptr = mxGetPr(a_out_m); // Get pointer to data area of the copy
*temp_ptr = *temp_ptr + (*temp_ptr*10); // Updates local copy directly
success = mexPutVariable("caller", "input_var", a_out_m); // Replace workspace variable
All of the above assumes that input_var exists, is a double scalar, etc, etc. To make your code robust you would generally put in checks to make sure the input is as expected before you dereference pointers, etc.
  2 Comments
jchaliss
jchaliss on 17 Jul 2013
Thanks James...your fix was correct and it did help solve the crashing of matlab,...now i understand that i need to improve my skill in using pointers rather than using them blindly...:-), thanks again...
...currently i'm assigning my class variable elements (say, input.input_var) to a local variable(input_var) and then updating the local variable from within the mex function and finally assigning the class variable back from the updated local...
the reason behind doing this overhead is simply because i do not know how to read a simple class/structure element directly from the mex function...how do i read these class/structure elements(variables) directly, and update them as well, from the mex function or is it better to go ahead with the current overhead?...thanks.
James Tursa
James Tursa on 17 Jul 2013
1) Is the class variable a new classdef type of object or the old @classname directory structure type of object?
2) Could you please post some simple m-code to show what you are doing, and also explain why you are doing some updating within a mex function?
You can access old style class variables (via mxGetField, mxSetField) and new style class variables (via mxGetProperty, mxSetProperty) directly within a mex routine. How one goes about doing this depends on your answers to the questions above and what you are attempting to do overall with the variable. I will warn you up front the details can get quite involved because of the variable data sharing that goes on within MATLAB, but we will cross that bridge when we come to it.

Sign in to comment.

More Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!