Angulandy2
Angulandy2

Reputation: 806

Matlab pass variable from one callback to another

I am new to matlab and I want to pass one variable to another :

Here is callback 1 where I write variable with 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);

And here I got another callback where I want that variable b would be my 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);

Upvotes: 2

Views: 307

Answers (1)

Zep
Zep

Reputation: 1576

You are halfway there with your guidata(hObject, handles);. You can use the handles structure to store data, like this:

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

Now you can access handles.b from your pushbutton1_Callback:

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

More info on guidata() and handles.

Upvotes: 3

Related Questions