Calling a callback function in GUI

3 ビュー (過去 30 日間)
Filip Kruczek
Filip Kruczek 2019 年 5 月 10 日
回答済み: Rik 2019 年 5 月 12 日
Hello to all,
This is my first attempt to generate a GUI. Its aim is to plot the diagrams of variables (saved in .mat file as structure with time). It has to be done by choosing the name of variable from dropdwon list. Below there is a part of a code. I have 2 types of variables sorted into two seperate lists.
ana_dd = uicontrol( 'Style', 'popup',...
'String', ana_vars,...
'Position', [20 340 100 50],...
'Callback', @ploting_ana);
misc_dd = uicontrol( 'Style', 'popup',...
'String', misc_vars,...
'Position', [20 300 100 50],...
'Callback', @ploting_misc);
function ploting_ana(~,~)
val=ana_dd.Value;
plot(T.(ana_vars{val}).time, ...
T.(ana_vars{val}).signals.values);
end
function ploting_misc(~,~)
val=misc_dd.Value;
plot(T.(misc_vars{val}).time, ...
T.(misc_vars{val}).signals.values);
end
As you see both ploting_ana(~,~) and ploting_misc(~,~) are pretty simmilar. There are only differences in the name of variable. I would like to ask if there is a possibility to call and define a callback function in a similar way how in regular function, so I would have sth like below. Then there won't be a need of defining two almost the same functions.
ana_dd = uicontrol( 'Style', 'popup',...
'String', ana_vars,...
'Position', [20 340 100 50],...
'Callback', @ploting(ana_vars));

採用された回答

Rik
Rik 2019 年 5 月 12 日
Your problem is not taking into account the full potential of callbacks. You throw away the handle to the object you're using. Your function should look something like this. (I would also discourage the use of nested functions, as they make it harder to keep track of your variables)
function ploting_misc(hObject,~)
val=hObject.Value;

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by