Error(s) callback uicontrol in a function

3 vues (au cours des 30 derniers jours)
83quarks
83quarks le 26 Fév 2019
Commenté : Jan le 27 Fév 2019
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 commentaire
Jan
Jan le 26 Fév 2019
Modifié(e) : Jan le 26 Fév 2019
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.

Connectez-vous pour commenter.

Réponse acceptée

83quarks
83quarks le 27 Fév 2019
Hello again,
ok i don't need a callback.
Now this is the code i want to have and which one is working as i want:
Thank you!
function [Select,fh_GUI,h] = f_Auswahlliste_GUI_v02()
fh_GUI=figure;
set(fh_GUI, 'position', [50 50 [220 120]*8]);
RPLT.Auswahlelemente1 = {'TOP1'; 'TOP2'; 'TOP3'};
RPLT.Auswahlelemente2 = {'TOPF6'; 'TOPF7'; 'TOPF8'};
%% Listbox-Elemente
h.h_list1_text = uicontrol('Style','Text','Position',[20 900 150 20],'String','xxxx1 auswählen','Visible','on');
h.h_list1 = uicontrol('style','list','Value',[],'max',length(RPLT.Auswahlelemente1),...
'min',1,'Position',[20 200 150 680],'Visible','on',...
'string',RPLT.Auswahlelemente1);
h.h_list2_text = uicontrol('Style','Text','Position',[320 900 150 20],'String','xxxx1 auswählen','Visible','on');
h.h_list2 = uicontrol('style','list','Value',[],'max',length(RPLT.Auswahlelemente2),...
'min',1,'Position',[320 200 150 680],'Visible','on',...
'string',RPLT.Auswahlelemente2);
uicontrol('Style', 'pushbutton', 'String', 'Get & Close',...
'Position', [1150 20 80 30],...
'Callback', {@closefig, fh_GUI});
uiwait(fh_GUI);
Select.List1 = f_Auswahllisteintern(h.h_list1);
Select.List2 = f_Auswahllisteintern(h.h_list2);
close(fh_GUI);
end
function output = f_Auswahllisteintern(Liste)
index = get(Liste, 'Value');
list = get(Liste, 'String');
output = list(index);
end
function closefig(buttonh,event,fh_GUI)
uiresume(fh_GUI); % Return to main program after Selection
end
  1 commentaire
Jan
Jan le 27 Fév 2019
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.

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 26 Fév 2019
Modifié(e) : Jan le 26 Fév 2019
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 commentaires
Jan
Jan le 26 Fév 2019
[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
Jan le 26 Fév 2019
Modifié(e) : Jan le 26 Fév 2019
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".

Connectez-vous pour commenter.

Catégories

En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange

Produits


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by