Calling a callback function in GUI

3 visualizaciones (últimos 30 días)
Filip Kruczek
Filip Kruczek el 10 de Mayo de 2019
Respondida: Rik el 12 de Mayo de 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));

Respuesta aceptada

Rik
Rik el 12 de Mayo de 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;

Más respuestas (0)

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by