How to check the value of a varriable of a subfunction?

2 views (last 30 days)
I've a function that calls another function. I can debug the program but I can't check the value of any variable of the called function after debugging. It shows me error. I want to use the command
assignin('base', 'my_variable', Value_of_variable)
but I don't know where to use it. Please help me.
  1 Comment
Stephen23
Stephen23 on 18 Oct 2015
Edited: Stephen23 on 18 Oct 2015
This question does not really make much sense: the whole point of functions is that that workspace and the variables inside it do not exist once the scope of the function is no longer required. Normally this means that when the function finishes running, the variables are gone. If you want to keep those variables around, simply pass them as output variables.

Sign in to comment.

Accepted Answer

David Sanchez
David Sanchez on 27 Jun 2013
Place the code in the sub_function that holds the variable you want to send to the workspace to see its value, just after the variable is created. In the sentence below, "my_variable" is the name of the variable you want to send to the workspace ('base') in order to check its value. "Value_of_variable" is the value of your variable.
assignin('base', 'my_variable', Value_of_variable)
For example, imagine that in your sub-function, there is a variable called voltage with a value of 12:
voltage = 12;
them, to send it to the workspace:
assignin('base', 'voltage', 12);
or
assignin('base', 'voltage', voltage);
  2 Comments
jin wang
jin wang on 18 Oct 2015
what if I don't know the exact value of the variable in the sub function? Then what other function can I use so that I can keep whatever I get in the sub function and see it after I run the main function.
John D'Errico
John D'Errico on 18 Oct 2015
You cannot recover a variable from a sub-function AFTER it has terminated. That would make no sense at all. Once a function terminates, every variable in it gets dumped into the bit bucket. Sadly, the bit bucket is a notoriously sloppy mess, and nothing ever comes out of it.
Instead, learn to use the debugging tools. Step into the sub-function when it is called, or put a stop at the important line.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 27 Jun 2013
Edited: Matt J on 27 Jun 2013
You should use DBSTOP instead, along with MATLAB debugging features described here.

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!