Is there an undocumented way to render a variable 'invisible' in matlab such that it still exists but does not show up in the workspace list?
One thing you can do is have global variables. An interesting property of these is that even when you clear the workspace they still exist in memory unless you clear global variables specifically. An example is below.
global hidden_var
hidden_var = 1;
clear
global hidden_var
hidden_var
I'm still not entirely sure why you would even want the feature but this is a way that you can "hide" variables from the workspace.
clear
will cause the variable to not appear when issuing the statement who
, but it will still show up in the global workspace with who global
; 2) After the clear
, you can't access the stored value until you redeclare with global hidden_var
, at which point it is fully visible in the workspace again; 3) A clear all
will clear even the global workspace, erasing the stored value. –
Aglaia The only way I can think of is to actually use a function, in the same way as MATLAB defines pi
, i
, and j
. For example:
function value = e
value = 2.718;
end
There will be no variable named e
listed in your workspace, but you can use it as though there were:
a = e.^2;
Technically, it's only "invisible" in the sense that functions like who
and whos
don't list it as a variable, but the function will still have to exist on your MATLAB path and can still be called by any other script or function.
One thing you can do is have global variables. An interesting property of these is that even when you clear the workspace they still exist in memory unless you clear global variables specifically. An example is below.
global hidden_var
hidden_var = 1;
clear
global hidden_var
hidden_var
I'm still not entirely sure why you would even want the feature but this is a way that you can "hide" variables from the workspace.
clear
will cause the variable to not appear when issuing the statement who
, but it will still show up in the global workspace with who global
; 2) After the clear
, you can't access the stored value until you redeclare with global hidden_var
, at which point it is fully visible in the workspace again; 3) A clear all
will clear even the global workspace, erasing the stored value. –
Aglaia I would suggest grouping variables in a structure as a workaround. Running the code below will only show up as mainVariable
in your workspace. The disadvantage is that you will have to type the whole thing to access the variables, but you can shorten the names.
mainVariable.actualVariable1 = 1 mainVariable.actualVariable2 = [2, 4] mainVariable.actualVariable3 = 'Hello World'
© 2022 - 2024 — McMap. All rights reserved.