Error(s) callback uicontrol in a function
3 次查看(过去 30 天)
显示 更早的评论
Hello,
i have one or more problems by getting values from a listboy back to main function. if its running as a script it worked.
I'm not using GUIDE oder app-builder and a beginner
There is one proplem by evaluating the callback if the main-program is a function, as shown in the following code-example:
[] = function f_Auswahlliste_GUI()
fh_GUI=figure;
set(fh_GUI, 'position', [50 50 [220 120]*8]);
RPLT.Auswahlelemente = {'TOP 1';'TOP2';'TOP3'};
h.h_list_text = uicontrol('Style','Text','Position',[20 900 150 20],'String','xxxx auswählen');
h.h_list = uicontrol('style','list','Value',[],'max',length(RPLT.Auswahlelemente),...
'min',1,'Position',[20 200 150 680],...
'string',RPLT.Auswahlelemente,'Callback','Select = f_Auswahlliste(h.h_list)');
% '[Select] = @f_Auswahlliste(h.h_List)'
% 'Select = f_Auswahlliste(h.h_list)'
h.h_pb = uicontrol('Style', 'pushbutton', 'String', 'Get & Close',...
'Position', [1150 20 80 30],...
'Callback', @cla);
uiwait(fh_GUI);
end
function output = f_Auswahlliste(Liste)
index=get(Liste,'Value');
list = get(Liste,'String');
output = {list{index}};
end
function cla(h,event)
uiresume(gcbf); % Return to main program after Selection
close;
end
Could somebody help me?
Thanks
1 个评论
Jan
2019-2-26
编辑:Jan
2019-2-26
This is not valid Matlab syntax:
[] = function f_Auswahlliste_GUI()
I guess, tha "[] = " is a typo. Please remove it.
You did not mention, what the problem is and what you want to achieve. "getting values from a listboy back" is not clear enough.
It is a bad idea to define a callback as string. This was outdated over 12 years ago and is only supported for backward compatibility:
'Callback', 'Select = f_Auswahlliste(h.h_list)'
I cannot guess, where you want the output Select to appear.
Do not redefine the builtin function cla.
采纳的回答
83quarks
2019-2-27
1 个评论
Jan
2019-2-27
By the way, until R2007b Matlab had the function popupstr, which could replace your f_Auswahllisteintern. You can find a replacement for this useful, but tiny function here: FEX: GetSelection
Replying fh_GUI and h is dangerous, becahsue you have closed the figure already, such that all handles are invalid. Then there is no reason to reply them as output.
更多回答(1 个)
Jan
2019-2-26
编辑:Jan
2019-2-26
With some bold guessing:
function output = f_Auswahlliste_GUI()
fh_GUI = figure('position', [50 50 [220 120]*8]);
RPLT.Auswahlelemente = {'TOP 1'; 'TOP2'; 'TOP3'};
h.h_list_text = uicontrol('Style', 'Text', ...
'Position', [20 900 150 20], ...
'String', 'xxxx auswählen');
h.h_list = uicontrol('style', 'listbox', ...
'Value', [], 'max', numel(RPLT.Auswahlelemente), 'min', 1, ...
'Position', [20 200 150 680],...
'string', RPLT.Auswahlelemente);
h.h_pb = uicontrol('Style', 'pushbutton', 'String', 'Get & Close',...
'Position', [1150 20 80 30],...
'Callback', {@myCallback, fh_GUI});
uiwait(fh_GUI);
index = get(h.h_list, 'Value');
list = get(h.h_list, 'String');
output = list(index);
close(fh_GUI);
end
function myCallback(ButtonH, Event, fh_GUI)
uiresume(fh_GUI);
end
2 个评论
Jan
2019-2-26
[MOVED from section for answers] (please post comments in the section for comments)
83quarks wrote:
Thanks for the button callback correction.
Sorry I tried to extract my code in an example - so its may be not looking so clear - could you understand the following problem:
The problem ist i have lots of lists an want the extraction of the Listbox-Values in an internal or external function:
output = f_Auswahlliste(inputlist)
index=get(Liste,'Value');
list = get(Liste,'String');
output = {list{index}};
But how must the callback of every listbox look like ??
h.h_list = uicontrol('style','list','Value',[],'max',length(RPLT.Auswahlelemente),...
'min',1,'Position',[20 200 150 680],...
'string',RPLT.Auswahlelemente,'Callback',???'want to call the "Select.values = f_Auswahlliste(..)"');
h.h_list2 .....
....
Jan
2019-2-26
编辑:Jan
2019-2-26
You did not mention yet, why you need a callback for the listbox. As far as I can guess, you want to open a figure, display a listbox, select some of its elements and press a button to finish the selction. This is done by my suggested code already. Therefore I do not know, what the callback of the listbox should do. "want to call the "Select.values = f_Auswahlliste(..)"" is not clear.
You can implement a callback by e.g.:
h.h_list = uicontrol('style','list','Value',[],'max',length(RPLT.Auswahlelemente),...
'min',1,'Position',[20 200 150 680],...
'string', RPLT.Auswahlelemente, ...
'Callback', @listCallback);
...
function listCallback(ListH, Event)
Value = get(ListH, 'Value');
Str = get(ListH, 'String');
Selected = Str(Value)
end
But there is nothing to do here. Better wait until the button was pressed and determine the selected strings afterwards to reply them as output of the GUI. This works with multiple listboxes also.
I have included this code in the GUI already:
index = get(h.h_list, 'Value');
list = get(h.h_list, 'String');
output = list(index); % more efficient than {list{index}}
so there is no need to call "an internal or external function".
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!