user2305193
user2305193

Reputation: 2059

How to hide a variable from workspace in matlab

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?

Upvotes: 11

Views: 3403

Answers (3)

Durkee
Durkee

Reputation: 778

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.

Upvotes: 5

MosGeo
MosGeo

Reputation: 1888

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'

Upvotes: 0

gnovice
gnovice

Reputation: 125854

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.

Upvotes: 15

Related Questions