フィルターのクリア

How to read a value of popup inside its callback

1 回表示 (過去 30 日間)
Michal
Michal 2014 年 7 月 13 日
回答済み: Michal 2014 年 7 月 13 日
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

採用された回答

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

その他の回答 (2 件)

Ben11
Ben11 2014 年 7 月 13 日
編集済み: Ben11 2014 年 7 月 13 日
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 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
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by