MATLAB Answers

How can I debug MATLAB code in my MATLAB Engine application?

5 views (last 30 days)
I realize I cannot use the debugger to debug MATLAB code while calling it from a MATLAB Engine application.
However, I need to verify that my variables are being passed in correctly. If they are, I then need to debug my code to see why my answers are not what I expect.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 20 Jun 2011
This change has been incorporated into the documentation in Release 2011a (R2011a). For previous releases, read below for any additional information:
You can debug MATLAB code by saving the workspace as you enter your MATLAB code and then later using that workspace to debug your code.
For example, you might have the following function:
function y=myfcn(x)
y=x+2;
end
You can change it to:
function y=myfcn(x)
save debug_myfcn.mat
y=x+2;
end
You can then call myfcn from your MATLAB Engine application using engEvalString after putting a variable into MATLAB using engPutVariable.
For example, in your C code you might have:
engPutVariable(ep, "aVar", myCmxArray);
engEvalString(ep, "result=myfcn(aVar)");
myCmxArrayResult = engGetVariable(ep,"result");
While examining the results of myCmxArrayResult, you might find that the result is not what you expect. Since you saved the workspace as you entered your function, you can now open MATLAB and load the saved workspace to see what data came into your function. You can then debug your function if needed from inside MATLAB.
You can do this by starting MATLAB and then loading the resulting debug_myfcn.MAT-file (which has x defined in it):
load debug_myfcn.mat
whos x
x
If x is different from what you expect, you know that most likely you created the mxArray myCmxArray incorrectly. If x is what you expect, you could set a breakpoint in myfcn and then debug it by calling:
myfcn(x)

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Tags

No tags entered yet.

Products