フィルターのクリア

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

1 回表示 (過去 30 日間)
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

採用された回答

Stephen23
Stephen23 2017 年 9 月 14 日
編集済み: Stephen23 2017 年 9 月 14 日
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 件のコメント
Aleksandar Petrov
Aleksandar Petrov 2017 年 9 月 15 日
thank you, it works!!
so the main tip is that the parameters must be declared before they are used in the function

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by