How to read a value of popup inside its callback
显示 更早的评论
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
采纳的回答
更多回答(2 个)
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
2014-7-13
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 个评论
Image Analyst
2014-7-13
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Entering Commands 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!