Variables in workspace from functions
조회 수: 64 (최근 30일)
이전 댓글 표시
I used to always start MATLAB code with 'function' and used to have the same name for the file.
function c = game(a,b)
c = a+b;
end
And then, I would save this file as game.m and call function from the command prompt like game(a,b) and I used to see all the variables in the workspace. But some days ago, I just stopped seeing the variables declared inside the function in my workspace and I only get to see the output that is passed in the main script.
I read many forums and they all say that variables inside the function so not show up in workspace. I used to do this and I am not sure what happened. Is there some change in the setting that I need to make.
Please help
댓글 수: 1
Stephen23
2017년 5월 2일
편집: Stephen23
2017년 5월 2일
Every function has its own workspace separate from the base workspace, and any variables in those function workspaces should be invisible to the outside world. That is the whole point of functions: a function encapsulates some functionality and provides a clearly specified input and output to utilize that functionality. For example, think of the mean function: do you know what internal variables it uses? Do you know their names or values? No, and there is no point in having these displayed, because the function is defined by its functional behavior, not by whatever internal variables are used to realize that functionality.
The only time that you should need to view the workspace of a function is when writing/debugging it, in which case you should use the correct tools for viewing that workspace, i.e. the debugging tools:
답변 (1개)
Walter Roberson
2017년 5월 2일
Variables inside a function are only visible in the workspace browser while the function is paused for debugging (or some function called from it is paused for debugging.)
I gather that in some older versions of MATLAB, the workspace browser may have been updated while the function was executing, but not paused. I also gather that as of R2016a that is no longer the case -- that it does not update until a debugging pause (or at least not until there is an opportunity for the graphics event queue to be processed.) I do not quite understand why this is the situation, as R2016a was the release in which the command window and associated components were redesigned to execute more independently of the execution engine, so I would have expected updates to be more flexible rather than less flexible.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!