Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
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

Akzeptierte Antwort

Stephen23
Stephen23 am 14 Sep. 2017
Bearbeitet: Stephen23 am 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.
  1 Kommentar
Aleksandar Petrov
Aleksandar Petrov am 15 Sep. 2017
thank you, it works!!
so the main tip is that the parameters must be declared before they are used in the function

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by