【问题标题】:Save the output of a function保存函数的输出
【发布时间】:2016-08-26 20:09:57
【问题描述】:

我已经能够将以下代码放在一起,以在每次按下空格键时找到一个时间戳。但是,我想不出将 S.tm 保存到工作空间的方法。代码如下:

function main
    S.tm = [];
    S.cnt = 0;   
    S.fh = figure('KeyPressFcn',@youPressedSomething,...
                  'menu','none',...
                  'pos',[400 400 320 50]);
    S.th = uicontrol('Style','text','Position',[10 10 300 30],...
            'String','You have not hit space yet');
 function  youPressedSomething(varargin)
    if strcmp(varargin{2}.Character,' ')
            S.tm = [S.tm now] 
            S.cnt = S.cnt + 1;
            set(S.th,'str',sprintf('You hit space %i!',S.cnt));

    end
  end
 end

【问题讨论】:

  • 不是因为你没有声明任何输出吗?见here
  • 这是我不确定的。这种功能我没用过,不知道怎么实现。
  • @A_C 不是只有在main函数结束后才将值返回到工作区吗?我认为必须在工作空间中创建句柄,将其传递给main,然后传递给yPS 进行更新。
  • @Crowley 即使我关闭图,输出也不会保存到工作空间。

标签: matlab user-interface save


【解决方案1】:

这里有两个主要选项。您可以使用assignin您的函数中将数据保存到主工作区。

function main
    S.tm = [];
    S.cnt = 0;   
    S.fh = figure('KeyPressFcn',@youPressedSomething,...
                  'menu','none',...
                  'pos',[400 400 320 50]);
    S.th = uicontrol('Style','text','Position',[10 10 300 30],...
            'String','You have not hit space yet');

    function  youPressedSomething(varargin)
        if strcmp(varargin{2}.Character,' ')
            S.tm = [S.tm now] 
            S.cnt = S.cnt + 1;

            set(S.th,'str',sprintf('You hit space %i!',S.cnt));

            %// Save as "timestamps" in the base workspace
            assignin('base', 'timestamps', S.tm);

        end
    end
end

或者更好的方法是使用waitfor 阻止函数的执行,直到图形关闭。然后你可以像普通的输出参数一样返回S.tm

function timestamps = main()
    S.tm = [];
    S.cnt = 0;   
    S.fh = figure('KeyPressFcn',@youPressedSomething,...
                  'menu','none',...
                  'pos',[400 400 320 50]);

    S.th = uicontrol('Style','text','Position',[10 10 300 30],...
            'String','You have not hit space yet');

    %// Wait until the figure is closed
    waitfor(S.fh);

    %// Save S.tm as timestamps and return
    timestamps = S.tm;

    function  youPressedSomething(varargin)
        if strcmp(varargin{2}.Character,' ')
            S.tm = [S.tm now] 
            S.cnt = S.cnt + 1;

            set(S.th,'str',sprintf('You hit space %i!',S.cnt));

            %// Save as "timestamps" in the base workspace
            assignin('base', 'timestamps', S.tm);

        end
    end
end

【讨论】:

    【解决方案2】:

    您可以尝试以这种方式编辑您的代码:

    function[S.fh]=main()
    %% Your Code
    
    function yPS(varargin)
      if...
      %% Your Code
      end
      set(S.fh,'UserData',S.tm);
    end %yPS
    end %main
    

    这应该将图形句柄返回到工作区并将其保存在内存中。然后,每当调用 yPS 函数时,它都会使用共享句柄 S.fh 到图形并(重新)设置 UsedData 属性,该属性专用于包含用户数据。

    要使用该函数,请调用它Foo=main

    get(Foo.fh,'UserData')
    
    ans = 
    
    []
    

    按几次空格键

    >> get(AA.fh,'userdata')
    
    ans =
    
    1.0e+005 *
    
      7.3645
      7.3645
      7.3645
    

    【讨论】:

    • 我按照你的建议做了,但它显示“tm”为空白。我确实对您的建议进行了一项更改。在 [S.fh]=main() 中,我使用了 [S]=main() 因为 MATLAB 不喜欢 S.fh 输出。
    • 仍然输出空白
    • 我现在真的很困惑。你是说我应该使用 function[Fig]=nestie() 并在不同的文件中调用它,对吗?我什么时候按空格键来记录时间戳?
    • 当我运行代码时,现在它只是存储我按空格键的次数,但它没有记录时间,所以我必须相应地编辑它。但是,代码只有在您保持图形打开时才有效,如果它关闭,则 get(Foo,'UserData') 会导致错误
    • 是的,如果关闭图窗,其数据结构将被删除。您可以尝试使用DeleteFcn 属性保存数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多