Matlab将变量从一个回调传递给另一个回调

时间:2018-05-14 07:00:31

标签: matlab matlab-guide

我是matlab的新手,我想将一个变量传递给另一个变量:

这里是回调1,我用输入写了变量:

function pushbutton2_Callback(hObject, eventdata, handles)

c = {'Enter image size:'};
title = 'Input';
dims = [1 35];
definput = {'500'};
answer = inputdlg(c,title,dims,definput);
disp(answer);
b = str2double(answer); // I want to pass this b to other callback
disp(b); 
guidata(hObject, handles);

在这里,我得到另一个回调,我希望变量 b 将是我的 c

function pushbutton1_Callback(hObject, eventdata, handles)

 h = randi([0 70],c,c); //here I want that c would be my b from another callback
    dlmwrite('myFile.txt',h,'delimiter','\t');


    [file,path] = uigetfile('*.txt');
    fileID = fopen([path,file],'r');
    formatSpec = '%d %f';
    sizeA = [c c];
    A = fscanf(fileID,formatSpec,sizeA);
    fclose(fileID);

    disp(A);
    image(A);
    saveas(gcf,'kazkas.png')
    %uiputfile({'*.jpg*';'*.png'},'File Selection');
guidata(hObject, handles);

1 个答案:

答案 0 :(得分:3)

你在guidata(hObject, handles);的中途。您可以使用handles结构来存储数据,如下所示:

function pushbutton2_Callback(hObject, eventdata, handles)

    % [...] your code
    handles.b = b;              % Store b as a new field in handles
    guidata(hObject, handles);  % Save handles structure

现在,您可以从handles.b

访问pushbutton1_Callback
function pushbutton1_Callback(hObject, eventdata, handles)     
    c = handles.b;
    % [...] your code

有关guidata()handles的更多信息。