| MATLAB Application Program Interface | Search  Help Desk |
| mexPrintf | Examples See Also |
ANSI C printf-style output routine
#include "mex.h" int mexPrintf(const char *format, ...);format, ...
printf-style format string and optional arguments.
This routine prints a string on the screen and in the diary (if the diary is in use). It provides a callback to the standard C 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