MATLAB Application Program Interface Reference | Help Desk |
mexPrintf
ANSI Cprintf
-style output routine
#include "mex.h" int mexPrintf(const char *format, ...);format, ...
ANSI C printf
-style format string and optional arguments.
printf
routine already linked inside MATLAB, and avoids linking the entire stdio
library into your MEX-file.
In a MEX-file, you must call mexPrintf
instead of printf
.
Consider a MEX-file named DispComp
that expects two input arguments and determines which of them has the larger first element.
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]) { mxArray *array_ptr; double value_in_first_array, value_in_second_array; /*Get the value of the first element in both input arrays.*/ value_in_first_array = mxGetScalar(prhs[0]); value_in_second_array = mxGetScalar(prhs[1]); if (value_in_first_array > value_in_second_array) mexPrintf("%g is greater than %g.\n", value_in_first_array, value_in_second_array); else mexPrintf("%g is not greater than %g.\n", value_in_first_array, value_in_second_array); }In MATLAB, create two vectors:
>> a=[53 2 17]; >> b=[65 14 23 99 57];Pass
v1
and v2
as arguments to DispComp
:
>> DispComp(v1, v2) 53 is not greater than 65.For an additional example, see
mexPrintf.c
in the mex
subdirectory of the examples
directory.
mexErrMsgTxt
, mexWarnMsgTxt