簡體   English   中英

Matlab將變量從一個回調傳遞到另一個

[英]Matlab pass variable from one callback to another

我是Matlab的新手,我想將一個變量傳遞給另一個變量:

這是回調1,其中我用input編寫了變量:

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);一半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()handles的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM