A question about C source MEX File
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
xie teng
el 6 de En. de 2017
Editada: James Tursa
el 9 de En. de 2017
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 ? ? ?

>>

>>
0 comentarios
Respuesta aceptada
Steven Lord
el 6 de En. de 2017
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 comentario
James Tursa
el 9 de En. de 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])) {
Más respuestas (0)
Ver también
Categorías
Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!