13

有谁知道在 MATLAB 中是否可以有一堆工作区?至少可以说,这将非常方便。

我需要这个来研究。我们有几个脚本以有趣的方式交互。函数有局部变量,但没有脚本......

4

3 回答 3

25

常规的 Matlab 函数调用堆栈本身就是一个工作区堆栈。仅使用函数是最简单的使用方法,而 Matlab 的写时复制使这种方法相当有效。但这可能不是你要问的。

工作空间和结构之间存在自然对应关系,因为相同的标识符对变量名和结构字段有效。它们本质上都是标识符 => Mxarray 映射。

您可以使用whosandevalin将工作区状态捕获到结构中。使用单元向量来实现它们的堆栈。(结构数组不起作用,因为它需要同质的字段名称。)堆栈可以存储在 appdata 中以防止它出现在工作区本身中。

这是此技术的推送和弹出功能。

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
于 2009-12-01T15:47:58.933 回答
7

It sounds like you'd like to switch back and forth between workspaces of variables. The best way I can think to do this is to use the SAVE, CLEAR, and LOAD commands to move sets of variables back and forth between MAT-files and the workspace:

save workspace_1.mat   %# Save all variables in the current workspace
                       %#   to a .mat file
clear                  %# Clear all variables in the current workspace
load workspace_2.mat   %# Load all variables from a .mat file into the
                       %#   current workspace
于 2009-12-01T05:37:46.830 回答
0

精彩的。(还没有发现使用 0 和 getappdata 记录在任何地方......所以这可能在未来消失。)已将 push & pop 添加到我的 util 库中,还有以下内容:

pop_workspace(keep_current)
% keep_current:  bool:  if true, current vars retained after pop
. . .
if (~keep_current)
     evalin('caller','clear');
end

一点创意,一个人可以只保留选定的变量,并避免在流行音乐上覆盖。我发现我的工作中还需要以下功能:

function pull_workspace(names)
%   pulls variablesin cell array names{} into workspace from stack without 
%   popping the workspace stack
%
%   pulled variable will be a local copy of the stack's variable, 
%   so modifying it will leave the stack's variable untouched.
%
    if (~exist('names','var') || isempty(names))
        pull_all = true;
    else
        pull_all = false;
%           if names is not a cell array, then user gave us 
%           just 1 var name as a string.  make it a cell array.
        if (~iscell(names))
            names = {names};
        end
    end

    % Peek at last workspace on stack
    c = getappdata(0, 'WORKSPACE_STACK');
    if isempty(c)
        warning('Nothing on workspace stack');
        return;
    end
    s = c{end};

    % Stick vars back in caller's workspace
    if (pull_all)
        names = fieldnames(s);
    end
    for i = 1:numel(names)
        assignin('caller', names{i}, s.(names{i}));
    end
end
于 2016-06-04T20:59:50.397 回答