Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 923
user2305193 Avatar asked Aug 15 '17 17:08

user2305193


People also ask

How do you remove a variable from a workspace in MATLAB?

To clear one or more specific variables from the current workspace, use clear name1 ... nameN . To clear all variables from the current workspace, use clear or clearvars . To clear all global variables, use clear global or clearvars –global .

How do I show variables in MATLAB workspace?

Command Window — To view the value of a variable in the Command Window, type the variable name. For the example, to see the value of a variable n , type n and press Enter. The Command Window displays the variable name and its value. To view all the variables in the current workspace, call the who function.

Is variable in workspace MATLAB?

The workspace contains variables that you create or import into MATLAB from data files or other programs. You can view and edit the contents of the workspace in the Workspace browser or in the Command Window. For more information, see Create and Edit Variables. Workspace variables do not persist after you exit MATLAB.


2 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.

like image 111
gnovice Avatar answered Sep 18 '22 01:09

gnovice


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.

like image 29
Durkee Avatar answered Sep 19 '22 01:09

Durkee