index in mxSetProperty or mxGetProperty
3 views (last 30 days)
Show older comments
Hi,
I'm beginning to work with MEX-C-files and I have the following question: What is the index input parameter for the mxGetProperty or mxSetProperty functions. The doc says "index of the desired element" - but the element is chosen via the parameter name, isn't it?
I have the following simple class in Matlab:
classdef testcalls
properties
a;
b;
c;
end
methods
function obj = testcalls(x,y,z)
obj.a = x+y;
obj.b = 42;
obj.c = rand(z, 1);
end
function obj = change(obj)
obj.a = obj.b;
obj.b = floor(obj.c(1,1) * 100);
obj.c = obj.c';
end
end
end
And now I have this (very simple) mex-file:
#include "mex.h"
#include "matrix.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
plhs[0] = mxDuplicateArray(prhs[0]);
mxSetProperty(plhs[0], 0, "a", mxCreateDoubleScalar(192));
}
In this file, I can easily change the third argument ("a") to "b" or "c" and everything works fine. But as soon as I change the second parameter to something different than 0, it crashes. So what kind of index is this? The first paramater is a pointer to a mxArray, not to an array of mxArrays...
0 Comments
Answers (1)
James Tursa
on 7 Aug 2013
Edited: James Tursa
on 7 Aug 2013
It crashes because there is only one element in the testcalls array being passed into the mex routine. I.e., I expect you are doing something like this:
x = testcalls;
y = mymexroutine(x); % 1-element array passed in
But you could add another element:
x(2) = testcalls;
y = mymexroutine(x); % 2-element array passed in
If you want to know how many elements the array being passed in has, use the mxGetNumberOfElements API function.
I will warn you that working with classdef objects inside a mex routine is very inefficient. The only routines that the API gives you, mxGetProperty and mxPutProperty, work with deep copies of the property. Not nice. To get any other behavior you have to use unofficial techniques, e.g. see this mxGetPropertyPtr function from the FEX:
0 Comments
See Also
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!