【问题标题】:Determine the most recently evaluated variable and save it to .mat-file确定最近评估的变量并将其保存到 .mat 文件
【发布时间】:2015-04-09 06:47:11
【问题描述】:

有没有办法在 Matlab 中找到最近评估的变量? whowhos 似乎没有为工作区中的变量添加时间戳。

这是一个用例。我想要一个通用函数'probe()',可以在 Matlab 脚本/代码的任何地方调用。我想将最近评估的变量保存到 .mat 文件中,而不必传递与正在保存的变量相关的任何自定义参数。这可能吗?

ans 接近我想要实现的目标,但它不可用,因为我的代码在分配的左侧有变量。

【问题讨论】:

  • 这是在 MATLAB 命令提示符下执行,还是在函数脚本文件中执行?
  • 欢迎来到 Stackoverflow!请考虑通过单击左侧的绿色复选标记来接受其中一个答案(如果它们对您有帮助)。这样,您就可以指示系统解决了您的问题。谢谢!

标签: matlab


【解决方案1】:

如果您在命令提示符中执行此操作,您可以通过use this post by gnovice 检索自您将 MATLAB 以文本数组打开后的整个命令历史记录。完成此操作后,您只需在 倒数第二行 行中搜索等号之前的变量...。假设您执行了 lhs 语句。您还需要考虑到您在命令提示符中回显变量而没有左侧语句。我们可以通过regexp轻松找到。

您需要在文本数组的倒数第二行进行搜索,因为 gnovice 捕获历史记录的代码需要额外的代码行。此代码被记录在历史记录中,这不是您想要的。因此,您需要查看倒数第二行/条目。

因此,这样做:

history = com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory; %// From gnovice's post
historyText = char(history);

lne = historyText(end-1,:); %// Get second last line

%// Do a regexp and retrieve the text with or without the equals sign
var = regexp(lne, '\w*', 'match');

%// Get first match which is the variable before any symbols or just the word
var = var{1};

这是一个简单的例子。这是我在尝试上述代码之前的完整命令历史记录:

>> A = rand(10,10);
>> B = A + 2;
>> D = B * 3;

运行上述代码后,我得到var

var = 

D

同样,如果我们只评估答案而不给左侧赋值:

>> A = rand(10,10);
>> B = A + 3;
>> A

运行上面的代码,我得到:

var = 

A

为了最终结束这一切,如果你想把这个变量保存到磁盘,你可以使用eval 语句来促进这一点:

>> name_of_file = 'file.mat';
>> eval(['save ' name_of_file ' ' var]);

上面的代码将采用您指定的文件名...所以在这种情况下它将是test.mat,然后使用var 作为您要保存的工作区的变量调用保存命令。

【讨论】:

    【解决方案2】:

    这是一个基本草图,使用函数dbstack

    function probe
    
    %// get filename of caller file and line where probe was called
    lineANDfile = dbstack;
    file = lineANDfile(end).file;
    linenum = lineANDfile(end).line;
    
    %// read caller m-file
    mLines = textread(file, '%s','delimiter', '\n');
    
    %// get line previous of the line where probe was called
    mLinesUntilProbeCall = mLines(1:linenum - 1);
    
    %// last non empty line before probe call -> line of interest
    mLine = mLines{ find( ~cellfun(@isempty,mLinesUntilProbeCall),1,'last') };
    
    %// get string (variable name) left of =
    varnameCell = textscan(mLine,'%s');
    
    %// isolate varnames
    getFouts = regexp(varnameCell{1}{1},'(?<=\[).+?(?=\])','match');
    if isempty(getFouts)
        varnames = varnameCell{1}(1);
    else
        varnames = strsplit( getFouts{1},',');
    end
    
    %// create struct with varnames as fields
    for ii= 1:numel(varnames)
        probeOut.(varnames{ii}) = evalin('base',varnames{ii});
    end
    
    save('probeOut.mat','probeOut');
    
    end
    

    您可以在如下脚本中调用:

    y = 5;
    xz = 42;
    
    
    probe  %// empty lines before call allowed!
    

    它将创建一个 .mat-文件,其中包含结构 probeOut 和字段 xz

    再次加载.mat-文件后:

    >> probeOut.xz
    
    ans =
    
        42
    

    如果您有多个输出参数,它也可以工作:

    y = 5;
    [xz,z] = deal(42,5);
    
    probe
    

    您的 .mat-文件将如下所示:


    案例

    y = 5;
    [xz] = deal(42,5);
    
    probe
    

    也包括在内。

    【讨论】:

      猜你喜欢
      • 2011-01-09
      • 2017-11-26
      • 1970-01-01
      • 2014-11-18
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多