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

Matlab pass variable from one callback to another

我是 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);

您的 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

现在您可以从 pushbutton1_Callback:

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

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