How to hide a variable from workspace in matlab
Asked Answered
V

3

11

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?

Vlissingen answered 15/8, 2017 at 17:36 Comment(3)
Can I ask: why?Leningrad
If it is invisible, how would one call it back?Barretter
You 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 aboutVlissingen
T
5

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.

Typhoon answered 15/8, 2017 at 19:8 Comment(1)
A 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.Aglaia
A
15

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.

Aglaia answered 15/8, 2017 at 17:43 Comment(2)
Can this be re-written as an anonymous function or does it actually have to be saved as a script?Vlissingen
An 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.Aglaia
T
5

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.

Typhoon answered 15/8, 2017 at 19:8 Comment(1)
A 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.Aglaia
T
0

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'

Trexler answered 15/8, 2017 at 18:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.