0

Lets just say I have 2 functuions:
1. [a b c]=func1()
2. [d]=func2(a)

I made a gui with 2 push buttons and copy pasted the two function calls after the call back function like this:

function pushbutton1_Callback(hObject, eventdata, handles)  
% hObject    handle to pushbutton5 (see GCBO)  
% eventdata  reserved - to be defined in a future version of  MATLAB  
% handles    structure with handles and user data (see GUIDATA)  
[a b c]=func1()

and for the second push button:

function pushbutton2_Callback(hObject, eventdata, handles)  
% hObject    handle to pushbutton5 (see GCBO)  
% eventdata  reserved - to be defined in a future version of MATLAB  
% handles    structure with handles and user data (see GUIDATA)  
[d]=func2(a)  

But when I run the gui it gives error that unknown function variable a but I defined a in func1.
Can anyone tell me what I am doing wrong or how to implement these functions properly.

2 Answers 2

2

You get the error because the workspace of each function you made (function1() and function2(a)) is not share with other functions. To circumvent this issue you have a few choices, 2 of which are the following:

1) Use the handles structure of the GUI to store variables and easily share them between callbacks and functions (Check here for infos)

2) Use setappdata and getappdata to associate application-defined data with your GUI figure. I'll let you read the docs about those, but here are 2 example using each method. I simplified function1() to only have 1 output for the demo, but the same principles apply with more output arguments.

1) handles structure

Since you are using GUIDE to make the GUI, note that the handles structure is conveniently accessible from any callback in your GUI (it is always passed as input arguments). Thus you don't need this line of code at the beginning:

handles = guidata(hfigure);

Play around with the GUI to see how it behaves.

Here is the code:

function TestFuncGUI
clc
clear all

hfigure  = figure('Position',[100 100 200 100],'Units','normalized');

handles.Button1= uicontrol('Style','push','String','1','Position',[40 50 50 30],'Callback',@(s,e) btn1_callback);
handles.Button2= uicontrol('Style','push','String','2','Position',[100 50 50 30],'Callback',@(s,e) btn2_callback);


guidata(hfigure,handles);

%// Callback for button 1
    function btn1_callback
        handles = guidata(hfigure);

        %// Assign output of function1 to handles.a variable.
        handles.a = function1();

        %// Update handles structure.
        guidata(hfigure,handles);
    end

%// function1
    function a = function1()
        handles = guidata(hfigure);

        %// Define a.
        a = magic(5);

        guidata(hfigure,handles);
    end

%// Callback for button 2. Call function2 and assign output to handles.d
    function btn2_callback
        handles = guidata(hfigure);

        %// IMPORTANT. Call function2 with handles.a
        handles.d = function2(handles.a);

        guidata(hfigure,handles);
    end

%// function2
    function d = function2(a)
        handles = guidata(hfigure);

        %// Dummy calculation and display result.
        d = a+10;

        disp(d)

        guidata(hfigure,handles);
    end
end

2) setappdata and getappdata

function TestFuncGUI2
clc
clear all

hfigure  = figure('Position',[100 100 200 100],'Units','normalized');

handles.Button1= uicontrol('Style','push','String','1','Position',[40 50 50 30],'Callback',@(s,e) btn1_callback);
handles.Button2= uicontrol('Style','push','String','2','Position',[100 50 50 30],'Callback',@(s,e) btn2_callback);


%// Callback for button 1
    function btn1_callback

       a = function1();
       %// Store "a" in the application data
       setappdata(hfigure,'a',a);
    end

%// function1
    function a = function1()

        %// Define a.
        a = magic(5);

        setappdata(hfigure,'a',a);

    end

%// Callback for button 2. Call function2 and assign output to handles.d
    function btn2_callback

        %// Retrieve "a" with getappdata.
        a = getappdata(hfigure,'a');

        %// Call function "d" with a.
        d = function2(a);

    end

%// function2
    function d = function2(a)

        %// Dummy calculation and display result.
        d = a+10;

        disp(d)

        guidata(hfigure,handles);
    end
end

So that's it. Have fun! If something is unclear please ask me!

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much I totally understand now. I wanna ask another thing I made an axes for the background image of gui. Now when I push the push button to subplot 2 images it doesn't pop up another window rather it displays the image on axes of background image. Any idea why?
You're welcome. If it helped you please mark the answer as accepted by clicking the green checkmark beside it. As for your question, you can create a new figure by calling figure() before the subplot, or use the Parent property of imshow to select the axes. Check the docs for more details.
0

Yes you defined a in pushbutton1_Callback and only there. In pushbutton2_Callback you are in a diffrent workspace, even if they are in the same File.

source: http://ch.mathworks.com/help/matlab/matlab_prog/base-and-function-workspaces.html


The solution is, since it looks like you are using guide, to use the handles struct:

function pushbutton1_Callback(hObject, eventdata, handles)  
% hObject    handle to pushbutton5 (see GCBO)  
% eventdata  reserved - to be defined in a future version of  MATLAB  
% handles    structure with handles and user data (see GUIDATA)  
[a b c]=func1()
handles.a=a; % write your Variable into the handles Struct
guidata(hObject,handles) % save the handles struct itselfe

the guide handles all the data over the handles-Struct and it is given in almost every callback by default. So you just can use it in the callback of the second button:

function pushbutton2_Callback(hObject, eventdata, handles)  
% hObject    handle to pushbutton5 (see GCBO)  
% eventdata  reserved - to be defined in a future version of MATLAB  
% handles    structure with handles and user data (see GUIDATA)  
[d]=func2(handles.a) % now get the Variable a from the handles struct

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.