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

조회 수: 7 (최근 30일)
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
Stephen23
Stephen23 2020년 5월 9일
"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:

댓글을 달려면 로그인하십시오.

채택된 답변

Nagaraja Shamsundar
Nagaraja Shamsundar 2020년 5월 9일
편집: Nagaraja Shamsundar 2020년 5월 9일
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
Mike M
Mike M 2020년 5월 9일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2020년 5월 9일
You could modify the function so it also returns the "temporary" variables or you could debug your code.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by