Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to push a MATLAB workspace onto a stack?

Does anyone know if it's possible to have a stack of workspaces in MATLAB? It would be very convenient, to say the least.

I need this for research. We have several scripts which interact in interesting ways. Functions have local variables, but not scripts...

like image 332
rlbond Avatar asked Dec 01 '09 02:12

rlbond


People also ask

Can a MATLAB function use workspace variable?

However, these workspaces are unique in two significant ways: Nested functions can access and modify variables in the workspaces of the functions that contain them. All of the variables in nested functions or the functions that contain them must be explicitly defined.

How do I access MATLAB workspace?

Open the Workspace Browser MATLAB Toolstrip: On the Home tab, in the Environment section, click Layout. Then, in the Show section, select Workspace. MATLAB command prompt: Enter workspace .

How do I change my workspace in MATLAB?

To open Workspace preferences, on the Home tab, in the Environment section, click Preferences. Select MATLAB > Workspace. Workspace preferences are not available in MATLAB Online™.

Why do we use workspace in 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.


1 Answers

The regular Matlab function call stack is itself a stack of workspaces. Just using functions is the easiest way to use one, and Matlab's copy-on-write makes this reasonably efficient. But that's probably not what you're asking.

There's a natural correspondence between workspaces and structs, since the same identifiers are valid for variable names and struct fields. They're both essentially identifier => Mxarray mappings.

You can use whos and evalin to capture workspace state to a struct. Use a cell vector to implement a stack of them. (A struct array won't work because it requires homogeneous field names.) The stack could be stored in appdata to prevent it from appearing in a workspace itself.

Here are push and pop functions for this technique.

function push_workspace()

c = getappdata(0, 'WORKSPACE_STACK');
if isempty(c)
    c = {};
end

% Grab workspace
w = evalin('caller', 'whos');
names = {w.name};
s = struct;
for i = 1:numel(w)
    s.(names{i}) = evalin('caller', names{i});
end

% Push it on the stack
c{end+1} = s;
setappdata(0, 'WORKSPACE_STACK', c);


function pop_workspace()

% Pop last workspace off stack
c = getappdata(0, 'WORKSPACE_STACK');
if isempty(c)
    warning('Nothing on workspace stack');
    return;
end
s = c{end};
c(end) = [];
setappdata(0, 'WORKSPACE_STACK', c);

% Do this if you want a blank slate for your workspace
evalin('caller', 'clear');

% Stick vars back in caller's workspace
names = fieldnames(s);
for i = 1:numel(names)
    assignin('caller', names{i}, s.(names{i}));
end
like image 125
Andrew Janke Avatar answered Nov 16 '22 00:11

Andrew Janke