如何从GUIDE GUI返回自定义值

时间:2018-03-20 18:37:30

标签: matlab matlab-guide

如何从GUIDE GUI而不是图形句柄返回自定义值?

value = myGui();

1 个答案:

答案 0 :(得分:0)

  1. 创建所需的GUI(用户控件,输入字段,处理逻辑等)

  2. 修改默认GUIDE生成的代码,如下所示(故意遗漏任何不需要修改的内容)。


  3. uiwait作为OpeningFcn()

    中的最后一行
    function myGui_OpeningFcn(hObject, eventdata, handles, varargin)
    
    ... % anything else you may want to do 
    
    % Wait here until user completes gui calculation.
    % Without pausing execution here, by default the figure handle 
    % would be immediately returned to the calling code.
    uiwait();
    

    注意:此将阻止您的调用代码中的执行


    打包输出值并在所需的回调中调用uiresume()

    function btnReturnValue_Callback(hObject, ~, handles)
    
    ...
    
    calcValue = doSomeCalculations();
    
    % Set 'output' value which will be returned from this figure.
    handles.output = calcValue;
    
    % Update the 'guidata' so the handles.output value just set will be saved.
    guidata(hObject, handles);
    
    % Resume execution which was blocked by OpeningFcn().
    uiresume();
    


    从其他代码中调用gui并存储结果值。

    value = myGui();

    返回的值可以是任何matlab对象(用户类,结构,单元格等)。