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

5 Ansichten (letzte 30 Tage)
Shaila parvin
Shaila parvin am 27 Jun. 2013
Bearbeitet: Stephen23 am 18 Okt. 2015
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 Kommentar
Stephen23
Stephen23 am 18 Okt. 2015
Bearbeitet: Stephen23 am 18 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

David Sanchez
David Sanchez am 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 Kommentare
jin wang
jin wang am 18 Okt. 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 am 18 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

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

Kategorien

Mehr zu Debugging and Analysis finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by