Variables created within function are cleared from workspace after reaching error

5 views (last 30 days)
I'm running a MATLAB R2020b script that creates several variables within a function. Somewhere in the function, I am receiving an error, which causes these variables created before the error to disappear from my workspace window. This is making it hard to debug my code. How can I make sure these variables are kept in the workspace even after the error?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 13 May 2021
Code erroring within a function will not store the function variables in the base workspace, so this is intended behavior. In order to see the values for the variables inside the function before the error, place breakpoints inside the function. Breakpoints will pause the execution of the program once it reaches the specified line, allowing you to observe the workspace variables at that point for debugging purposes.
For more information on setting breakpoints in MATLAB, please read the following documentation:
  1 Comment
Steven Lord
Steven Lord on 13 Oct 2022
If you set a standard breakpoint on a line inside the loop that is correct. In that case I would suggest setting an "error breakpoint" (see the Error Breakpoints section on the documentation page to which the MathWorks Support Team linked) which will cause MATLAB to enter debug mode exactly when the error occurs (in the case of a loop, it would only break on the line and in the iteration where MATLAB throws the error.)

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!