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?
-
2Can I ask: why? – Ander Biguri Aug 15 '17 at 17:37
-
If it is invisible, how would one call it back? – Ratbert Aug 15 '17 at 17:42
-
1You could call it back by actually calling the variable, which is simply 'not shown' in the workspace view. The use is to get to know the undocumented java part of matlab better (there is no profound 'why'), it's a part of matlab that is a bit more obscure to learn about – user2305193 Aug 15 '17 at 18:50
3 Answers
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.

- 125,304
- 15
- 256
- 359
-
Can this be re-written as an anonymous function or does it actually have to be saved as a script? – user2305193 Aug 15 '17 at 18:52
-
1An anonymous function would show up as a variable in the workspace, so it would have to be saved in a .m file on the path. – gnovice Aug 15 '17 at 18:54
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.

- 778
- 6
- 14
-
1A couple caveats: 1) A `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. – gnovice Aug 15 '17 at 20:28
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'

- 1,012
- 9
- 9