How to save passed value between two functions(callbacks) after closing the GUI?

时间:2018-01-15 18:19:31

标签: matlab function user-interface callback

I need to use output from first function as input to second function (callback). This is not a problem if I don't close the GUI but I want to use first callback, save value, close GUI and then open GUI again and use that value in second function. I hope you guys understand.

I tried to use CloseRequestFcn and OpeningFcn with setappdata/getappdata or save/load but either this can't be done or I'm doing something wrong.

I had few different errors like "Reference to non-existent field " or similar but still stuck.

1 个答案:

答案 0 :(得分:0)

我看到两个选项:

  1. 使用全局变量:使用global将变量声明为全局变量。按“保存”按钮和/或关闭GUI时写入此变量。然后在打开GUI和/或按下“加载”按钮时从全局读取。
  2. 使用文件:使用saveload函数将Matlab变量存储和检索到文件中。请参阅Matlab文档,了解如何仅存储特定变量。
  3. 全局变量示例:

    global testvar
    
    if isempty(testvar)
        testvar = 0;
    end
    
    disp(datestr(now))
    disp(testvar)
    
    testvar = testvar + 1;
    

    文件示例:

    if exist('testfile.mat', 'file')
        load('testfile', 'testvar')
    else
        testvar = 0;
    end
    
    disp(datestr(now))
    disp(testvar)
    
    testvar = testvar + 1;
    
    save('testfile', 'testvar')