How to pass parameters computed in different functions separately into another function?

2 views (last 30 days)
Hello,
I have three uicontrol popupmenus and I am supposed to select one item from all popupmenus. Each time I select an item, the appropriate Callback function computes a value (same parameter). After selecting the three items from all three different popupmenus, I press a pushbutton to compute another value (some parameter) which is computed by using the parameters from the previously selected items. here is how it looks like:
Selection1 = uicontrol('Style','popupmenu',...
'String',{'Selection1 ';'Selection2 ';'Selection3'},...
'Callback',@Selection1_Callback);
Selection2 = uicontrol('Style','popupmenu',...
'String',{'Selection4 ';'Selection5 ';'Selection6'},...
'Callback',@Selection2_Callback);
Selection3 = uicontrol('Style','popupmenu',...
'String',{'Selection7';'Selection8';'Selection9'},...
'Callback',@Selection3_Callback);
pushbutton = uicontrol('Style', 'pushbutton',...
'String', 'Compute', ...
'Callback' @pushbutton_Callback);
edit = uicontrol('Style', 'edit',...
'String', '');
function Selection1_Callback (hObject, eventdata, handles)
here I compute parameter 1
end
function Selection2_Callback (hObject, eventdata, handles)
here I compute parameter 2
end
function Selection3_Callback (hObject, eventdata, handles)
here I compute parameter 3
end
function pushbutton_Callback (hObject, eventdata, handles)
here I use parameters 1, 2, 3 in order to compute parameter 4
set(edit, 'String', Parameter4);
end
So how I can pass parameters 1, 2, and 3 into the pushbutton_Callback function so I could compute parameter 4 and then show it in edit box?
Thank you,
Aleksandar

Accepted Answer

Stephen23
Stephen23 on 14 Sep 2017
Edited: Stephen23 on 14 Sep 2017
I think that nested functions are the simplest and most intuitive way of passing data between GUI callback functions:
function mainGUI()
...
p1 = [];
p2 = [];
p3 = [];
%
function Selection1_Callback (hObject, eventdata)
p1 = ...compute parameter 1
end
%
function Selection2_Callback (hObject, eventdata)
p2 = ...compute parameter 2
end
%
function Selection3_Callback (hObject, eventdata)
p3 = ...compute parameter 3
end
%
function pushbutton_Callback (hObject, eventdata)
p4 = p1 + p2 + p3;
set(edit, 'String', num2str(p4))
end
%
end % end of mainGUI function
For other ways of passing data between callback functions you can read the MATLAB documentation:
Note: avoid global and assignin: these will not help you to write good code.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!