传递变量从按钮到按钮matlab

问题描述:

晚上好人们,传递变量从按钮到按钮matlab

我想知道如何将一个变量从一个按钮传递给另一个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是要在函数按钮5中知道的变量。

+0

试图澄清问题。更正的语法 – LoicTheAztec

您可以将变量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 

另外,还可以使用guidata存储数据,因此不推荐在handles结构虽然这非常大量的数据,你会发现一个性能命中。

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