A question about C source MEX File

조회 수: 5 (최근 30일)
xie teng
xie teng 2017년 1월 6일
편집: James Tursa 2017년 1월 9일
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 ? ? ?
>>
>>

채택된 답변

Steven Lord
Steven Lord 2017년 1월 6일
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
James Tursa
James Tursa 2017년 1월 9일
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])) {

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by