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

2 vues (au cours des 30 derniers jours)
Shaila parvin
Shaila parvin le 27 Juin 2013
Modifié(e) : Stephen23 le 18 Oct 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 commentaire
Stephen23
Stephen23 le 18 Oct 2015
Modifié(e) : Stephen23 le 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.

Connectez-vous pour commenter.

Réponse acceptée

David Sanchez
David Sanchez le 27 Juin 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 commentaires
jin wang
jin wang le 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 le 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.

Connectez-vous pour commenter.

Plus de réponses (1)

Matt J
Matt J le 27 Juin 2013
Modifié(e) : Matt J le 27 Juin 2013
You should use DBSTOP instead, along with MATLAB debugging features described here.

Catégories

En savoir plus sur Debugging and Analysis dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by