A question about C source MEX File

12 views (last 30 days)
According to C Syntax, the function in the first pictures below should return nothing because of "void", and there are should be four inputs(x,y,z,n), but when testing the mex file in the next picture, the function returns an output and there are only two inputs. Why ? ? ?
>>
>>

Accepted Answer

Steven Lord
Steven Lord on 6 Jan 2017
You're referring to the example on this documentation page?
You are correct that the C function named arrayProduct called by the mexFunction gateway function defined in arrayProduct.c is defined to accept four inputs and return no outputs.
When you compile the MEX-file source code arrayProduct.c to generate the MEX-file arrayProduct (with a platform-specific extension) and call that MEX-file from MATLAB, the inputs and outputs passing between MATLAB and the MEX-file are passed into and out of the mexFunction gateway function, not directly into and out of the C function arrayProduct.
See the "Read Input Data" and "Prepare Output Data" sections on the documentation page for the code in mexFunction that accepts the input from MATLAB and returns the output to MATLAB.
So the flow of data is:
MATLAB --> arrayProduct MEX-file / mexFunction C function
arrayProduct MEX-file / mexFunction C function --> arrayProduct C function
arrayProduct C function --> mexFunction C function / arrayProduct MEX-file
mexFunction C function / arrayProduct MEX-file --> MATLAB
  1 Comment
James Tursa
James Tursa on 9 Jan 2017
FYI that example has a bug in it and will likely crash MATLAB if a sparse vector is passed in as the 2nd argument. The corrected checking code is:
/* make sure the second input argument is type double */
if( !mxIsDouble(prhs[1]) ||
mxIsSparse(prhs[1]) || // <-- added this check
mxIsComplex(prhs[1])) {

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

Tags

Community Treasure Hunt

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

Start Hunting!