Print from C file into Command window via MexFile

조회 수: 8 (최근 30일)
Hendrik Lorenz
Hendrik Lorenz 2019년 6월 18일
편집: James Tursa 2019년 6월 18일
I am executing a C function via a mex file in Matlab. I understood that I can output messages from the mex file using mexPrintf as demonstrated in this example:
/*=================================================================
* mexfunction.c
*
* This example demonstrates how to use mexFunction. It returns
* the number of elements for each input argument, providing the
* function is called with the same number of output arguments
* as input arguments.
* This is a MEX-file for MATLAB.
* Copyright 1984-2011 The MathWorks, Inc.
* All rights reserved.
*=================================================================*/
#include "mex.h"
void
mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
int i;
/* Examine input (right-hand-side) arguments. */
mexPrintf("\nThere are %d right-hand-side argument(s).", nrhs);
for (i=0; i<nrhs; i++) {
mexPrintf("\n\tInput Arg %i is of type:\t%s ",i,mxGetClassName(prhs[i]));
}
/* Examine output (left-hand-side) arguments. */
mexPrintf("\n\nThere are %d left-hand-side argument(s).\n", nlhs);
if (nlhs > nrhs)
mexErrMsgIdAndTxt( "MATLAB:mexfunction:inputOutputMismatch",
"Cannot specify more outputs than inputs.\n");
for (i=0; i<nlhs; i++) {
plhs[i]=mxCreateDoubleMatrix(1,1,mxREAL);
*mxGetPr(plhs[i])=(double)mxGetNumberOfElements(prhs[i]);
}
}
However I would like to print directly from the c-file to Matlab Command Window. Is that possible?
The reason why I want to do that:
I want to raise an error/message if there occur NaN values in my numeric computations within the c-file to determine when and at which point in the code they appear. Since they propagate through the calculations and possibly make the whole function crash it is not enough for me to hand back a NaN-flag variable to the mexFunction after the c-function was executed.
Since this is just for debugging I also don't care about performance. What I would like to avoid though is to start the Visual studio debugger and attach the Matlab process to it as described here (since this is tedious): https://de.mathworks.com/help/matlab/matlab_external/debugging-on-microsoft-windows-platforms.html
  댓글 수: 1
dpb
dpb 2019년 6월 18일
So, what's the question? The sample code illustrates both just an informative message plus an error message. The latter also terminates the mex file but if don't want quite so big a club for debugging then mexWarnMsgIdAndTxt won't.
You can output whatever you wish from the context within the function at that point...I forget what NaN control you have within the mex environment to prevent the automagic error exit...

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

채택된 답변

James Tursa
James Tursa 2019년 6월 18일
편집: James Tursa 2019년 6월 18일
"However I would like to print directly from the c-file to Matlab Command Window. Is that possible?"
Yes, that is what mexPrintf( ) does, so I am not sure if I understand your problem yet.
If you are searching for NaN values then you may find the mxIsNaN( ) function useful:

추가 답변 (0개)

카테고리

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

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by