How do you output the values of temporary variables from a function when it runs?

8 views (last 30 days)
I am trying to better understand this function:
It gives an example of how to run the function as:
sos = adf(130.81,0.00015,4300,0.85);
If I input the full function and run this example line, it seems to work fine. If I then just type "sos" on a new line it outputs the values of "sos" as expected.
But how can I see the values of arrays/vectors/variables that were used in the process of creating this "sos" array? Eg. If you look at the code, one of the things it runs along the way is:
phik = pi*[1:2:2*nap-1];
How can I see the values of "phik"? If I just type "phik" after running the script as above it tells me the variable is undefined. But "phik" had to be defined in the process of creating "sos" since it's part of the function. Is there any way to get it to remember or store "phik" when it runs through the script or output it once it's done so I can see what it put in there along the way? How do I see "phik"?
Thanks.
  1 Comment
Stephen23
Stephen23 on 9 May 2020
"How do you output the values of temporary variables from a function when it runs?"
That is exactly what debugging tools are for.
All programming languages have debugging tools because programmers want to look at what their code does when it is run. And so they develop tools to do exactly that:

Sign in to comment.

Accepted Answer

Nagaraja Shamsundar
Nagaraja Shamsundar on 9 May 2020
Edited: Nagaraja Shamsundar on 9 May 2020
If you remove the semicolon at the end of the line containing "phik = ...", the value(s) of phik will be displayed to the console after that line is executed. If that line is executed many times, the resulting output may be excessive. In that case, you may wish to retain the semicolon and, after that line, add a conditional statement such as
if (condition), disp(phik), end
You can also make phik a global variable, and display its value at the end in the Matlab command window or any other script or function.
  1 Comment
Mike M
Mike M on 9 May 2020
Great. I found I can also just put "phik" by itself on its own line in the function code any point after it's defined and that will debug it as output at that point. Thanks.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 9 May 2020
You could modify the function so it also returns the "temporary" variables or you could debug your code.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by