make all variables in function stored/global/accesible

1 view (last 30 days)
I have a function that has an error. It takes 10 minutes to reach the error in a loop. I dont know how to debug it unless I can make all the variables in that function stored at workspace. Is there a setting for this ? that file has over 200 variables

Answers (2)

Adam
Adam on 19 Dec 2016
Edited: Adam on 19 Dec 2016
Use
dbstop if error
to stop the code at the point in the function where the error is. I can't imagine why you would ever want 200 variables in a single function, but that is another matter entirely.
If you wish you can dump them all to a .mat file using
save( filename )
at some point, including at the point the above will stop in your code.
The whole point of a function though (well, one of the key points, at least) is to encapsulate the variables within a sealed workspace of the function and not contaminate the workspace that called the function so debugging with stop on errors is the simplest way.

Steven Lord
Steven Lord on 19 Dec 2016
MATLAB has a debugging system that you can use to stop on a particular line in a function or when an error occurs. This will stop as the function executes, so you have access to all the variables that are present in the workspace and can use them to diagnose the cause of the problem.

Tags

Community Treasure Hunt

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

Start Hunting!