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

5 visualizaciones (últimos 30 días)
Shaila parvin
Shaila parvin el 27 de Jun. de 2013
Editada: Stephen23 el 18 de Oct. de 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 comentario
Stephen23
Stephen23 el 18 de Oct. de 2015
Editada: Stephen23 el 18 de Oct. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

David Sanchez
David Sanchez el 27 de Jun. de 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 comentarios
jin wang
jin wang el 18 de Oct. de 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 el 18 de Oct. de 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.

Iniciar sesión para comentar.

Más respuestas (1)

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

Categorías

Más información sobre Debugging and Analysis en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by