Borrar filtros
Borrar filtros

Variables in workspace from functions

67 visualizaciones (últimos 30 días)
Pushkar Kadam
Pushkar Kadam el 2 de Mayo de 2017
Editada: Stephen23 el 2 de Mayo de 2017
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 comentario
Stephen23
Stephen23 el 2 de Mayo de 2017
Editada: Stephen23 el 2 de Mayo de 2017
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:

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 2 de Mayo de 2017
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.

Categorías

Más información sobre Workspace Variables and MAT-Files 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