How to check the value of a varriable of a subfunction?
5 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Stephen23
2015 年 10 月 18 日
編集済み: Stephen23
2015 年 10 月 18 日
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.
採用された回答
David Sanchez
2013 年 6 月 27 日
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 件のコメント
jin wang
2015 年 10 月 18 日
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
2015 年 10 月 18 日
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.
参考
カテゴリ
Help Center および File Exchange で Debugging and Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!