将变量从按钮传递到按钮matlab

时间:2016-05-14 19:26:09

标签: matlab matlab-figure

晚上好人,

我想知道如何在matlab中将变量从按钮传递到另一个按钮。这是我的代码:

    function pushbutton4_Callback(hObject, eventdata, handles)
    [filename, pathname] = uigetfile('*.*', 'Pick a MATLAB code file','MultiSelect', 'on');
    fullfilename=fullfile(pathname,filename);
    b=importdata(fullfilename);
    set(handles.edit7,'string',fullfilename);


    function pushbutton5_Callback(hObject, eventdata, handles)
    mamamoa=load('best_network.mat');
    A=mamaoa(b);
    set(handles.edit1,'string',A);

变量b是函数pushbutton5中要知道的变量。

1 个答案:

答案 0 :(得分:0)

您可以将变量b保存在图中的appdata内。

function pushbutton4_Callback(hObject, eventdata, handles)
    [filename, pathname] = uigetfile('*.*', 'Pick a MATLAB code file','MultiSelect', 'on');
    fullfilename = fullfile(pathname,filename);
    b = importdata(fullfilename);
    set(handles.edit7, 'string', fullfilename);

    %// Store b within the appdata
    setappdata(handles.hfig, 'b', b);
end

function pushbutton5_Callback(hObject, eventdata, handles)
    mamamoa = load('best_network.mat');

    %// Retrieve b from the appdata
    b = getappdata(handles.hfig, 'b');      
    A = mamaoa(b);
    set(handles.edit1,'string',A);
end

或者,您可以使用guidatahandles结构存储数据,但不建议将其用于非常大量的数据,因为您会注意到性能受损。

function pushbutton4_Callback(hObject, eventdata, handles)
    [filename, pathname] = uigetfile('*.*', 'Pick a MATLAB code file','MultiSelect', 'on');
    fullfilename = fullfile(pathname,filename);

    %// Store it within handles.b
    handles.b = importdata(fullfilename);

    set(handles.edit7, 'string', fullfilename);

    %// Update the guidata
    guidata(handles.hfig, handles);
end

function pushbutton5_Callback(hObject, eventdata, handles)
    mamamoa = load('best_network.mat');
    A = mamaoa(handles.b);
    set(handles.edit1,'string',A);
end