How to read a value of popup inside its callback

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

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

Più risposte (2)

Ben11
Ben11 il 13 Lug 2014
Modificato: Ben11 il 13 Lug 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 il 13 Lug 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 Commento
Image Analyst
Image Analyst il 13 Lug 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.

Accedi per commentare.

Categorie

Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by