How to read a value of popup inside its callback

4 vues (au cours des 30 derniers jours)
Michal
Michal le 13 Juil 2014
Réponse apportée : Michal le 13 Juil 2014
Hello,
I am currently working on a GUI and got stacked at some point. I want to read the value of selected popup element inside its callback to change value of some other GUI component. Below the code that attempts to display the selected element
S.FromBlockPopUp = uicontrol('Style', 'popup',...
'String', ' ',...
'fontsize',10,...
'position',[400 200 100 25],...
'callback',{@update_text,S});
function [] = update_text(varargin)
S = varargin{3}; % Get the structure.
disp(getCurrentPopupString(S.FromBlockPopUp))
function str = getCurrentPopupString(hh)
list = get(hh,'String');
val = get(hh,'Value');
if iscell(list)
str = list{val};
else
str = list(val,:);
end
After execution I got the error:
Undefined variable "S" or class "S.FromBlockPopUp".
Unfortunately I am not able to read selected value as the struct S does not have a handle to that popup (It was created while adding the callback, hence the callback can't access it).
It seems a bit weird that I can't read it. Do you have any suggestion how it can be solved? Do you know any workarounds?
BR, Michal

Réponse acceptée

Michal
Michal le 13 Juil 2014
Solved:
S.FromBlockPopUp = uicontrol('Style', 'popup',...
'String', ' ',...
'fontsize',10,...
'position',[400 200 100 25]);
set(S.FromBlockPopUp,'callback',{@update_text,S});

Plus de réponses (2)

Ben11
Ben11 le 13 Juil 2014
Modifié(e) : Ben11 le 13 Juil 2014
Maybe using setappdata and getappdata? You could store your structure and retrieve it among the callbacks of your GUI.
eg.
setappdata(HandlestoYourGUI,'S',S);
at the end of a callback
and then
S = getappdata(HandlestoYourGUI,'S');
at the beginning of another in which you need to use s.

Image Analyst
Image Analyst le 13 Juil 2014
You can use GUIDE. Then it's trivial -- in the popup callback (or anywhere else that you have access to handles, like other callback functions) just do:
selectedItem = get(handles.popup1, 'Value'); % The item number that is selected.
allTextItems = get(handles.popup1, 'String'); % A list of the strings in the popup
selectedText = allTextItems{selectedItem}; % Text of whatever item number they selected.
  1 commentaire
Image Analyst
Image Analyst le 13 Juil 2014
It's definitely a lot harder if you're going to "roll your own" and take care of all those nitty gritty details yourself, as you found out. But if you're one of those who insist on doing it the hard way, see the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F Good luck.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by