Calling a callback function in GUI

1 view (last 30 days)
Filip Kruczek
Filip Kruczek on 10 May 2019
Answered: Rik on 12 May 2019
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));

Accepted Answer

Rik
Rik on 12 May 2019
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;

More Answers (0)

Categories

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

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!