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!