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 Center および File ExchangeWrite 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