pushbutton callback doesn't work

7 views (last 30 days)
Ali
Ali on 26 Sep 2022
Edited: Benjamin Kraus on 3 Oct 2022
i have a gui that i want to do a specific task when i press on the pushbutton ok which is to give me the number of the selected items in the two listbox that are in the gui for that i use this specific code :
function res=kgexec4e(action,cm,cp,cn,OptPref,OptAff)
%kgexec4e Création de la boite de dialogue "Choix Export" associée à
%kgexec4
% Detailed explanation goes here
persistent res1 Varlist1 Varlist2 %[] Initialement
%Open GUI
H1 = kgui4e;
%Calculate the barre listbox content
Varlist1 = cn.Barres.Nom(2:end);
%Calculate the element listbox content
Varlist2 = cn.Elements.Nom(1:end);
%%%the listbox takes the value of one array each
set(findobj(H1,'Tag','Barres'),'String',Varlist1)
set(findobj(H1,'Tag','Element'),'String',Varlist2)
res = res1
switch action
case 'OptionsOK' %When the pusbutton ok is pressed
nb = get(findobj(H1,'Tag','Barres'),'Value'); %find the selected item in the listbox barres
ne = get(findobj(H1,'Tag','ELement'),'Value');%find the selected item in the listbox element
close(gcbf)
end
end
The gui is set in another .m file called kgui4e.m and i set the pushbutton this way
uicontrol(h0,'Style','pushbutton',...
'Callback','kgexec4e',...
'Position',[250 30 65 35],...
'String','OK',...
'Tag','OptionsOK');
and my problem is when i remove the switch and case i will get the item that are selected when the gui opens and not after i press ok and when i use this code i will get this error message :
Error in Caviar\private\kgui4e (line 6)
blanc=[1 1 1];
Output argument "h0" (and maybe others) not assigned during
call to "C:\Users\223080038\Documents\m\CAVIAR3.1.2
_m_original\R2012a\Caviar\private\kgui4e.m>kgui4e".
Error in kgexec4e (line 8)
H1 = kgui4e;
Error while evaluating uicontrol Callback
which i don't understand if anyone could lighten me on the probleme it would be a great help.
PS : I work on matlab 2012a
  2 Comments
Ali
Ali on 27 Sep 2022
So the callback for the ok button is at this part of the code :
switch action
case 'OptionsOK' %When the pusbutton ok is pressed
nb = get(findobj(H1,'Tag','Barres'),'Value'); %find the selected item in the listbox barres
ne = get(findobj(H1,'Tag','ELement'),'Value');%find the selected item in the listbox element
close(gcbf)
end
The beginning of the code just calls the kgui4e function which launches the gui. And in the kgui4e (which represents the gui) i put the pushbutton ok at the end of it and i try to use it the other function to do a specific task when someone press on it.
function h0=kgui4e
%KGUI4e création boite de dialogue pour les options d'export de KGEXEC4
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
blanc=[1 1 1];
gris=blanc*0.75;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Show window if it already exist
hFig=findobj(allchild(0),'Tag','kgui4e');
if ~isempty(hFig), figure(hFig(1)), return, end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%chaines ds ttes les langues
ch00={
'Choix des valeurs à exporter'
'Exported values choice'};
ch01={
'Choix barre'
'Bar'};
ch02={
'Choix élément'
'Element'};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create figure window
h0=figure('Units','points', ...
'FileName','kgui4e.m', ...
'HandleVisibility','on', ...
'Color',gris,...
'DefaultUiControlBackgroundColor',gris,...
'IntegerHandle','on', ...
'MenuBar','none', ...
'Name','Choix des valeurs à exporter', ...
'Position',[500 500 500 500],...
'NumberTitle','off', ...
'Resize','off', ...
'Tag','kgui4e',...
'ToolBar','none');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Create buttons figure : choix toggle
uicontrol(h0,'Style','frame',...
'Position',[44 75 215 71]);
uicontrol(h0,'Style','checkbox',...
'Position',[52 80 200 20],...
'String','option 2 : Valeurs Impédance',...
'Tag','Imped1',...
'Value',1);
uicontrol(h0,'Style','checkbox',...
'Position',[52 120 200 20],...
'String','option 1 : Valeurs Spectre tension',...
'Tag','SpectreV',...
'Value',1);
uicontrol(h0,'Style','frame',...
'Position',[414 75 215 71]);
uicontrol(h0,'Style','checkbox',...
'Position',[422 80 200 20],...
'String','option 2 : Valeurs Spectre courant',...
'Tag','SpectreI',...
'Value',1);
uicontrol(h0,'Style','checkbox',...
'Position',[422 120 200 20],...
'String','option 1 : Valeurs Spectre tension',...
'Tag','SpectreVE',...
'Value',1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Create 2 List box of Barre and Element
uicontrol(h0,'Style','text',...
'Position',[50 495 200 20],...
'Tag','TitreBarre',...
'String','Liste des Barres');
uicontrol(h0,'Style','listbox',...
'Position',[50 150 200 350],...
'Tag','Barres',...
'max',1,...
'min',1);
uicontrol(h0,'Style','text',...
'Position',[420 495 200 20],...
'Tag','TitreElement',...
'String','Liste des Elements');
uicontrol(h0,'Style','listbox',...
'Position',[420 150 200 350],...
'Tag','ELement',...
'max',1,...
'min',1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Create 2 pushbuttons OK and Close
uicontrol(h0,'Style','pushbutton',...
'Callback','kgexec4e',...
'Position',[250 30 65 35],...
'String','OK',...
'Tag','OptionsOK');
uicontrol(h0,'Style','pushbutton',...
'callback','close(gcbf)',...
'Position',[320 30 65 35],...
'String','Annuler',...
'Tag','An');
movegui(h0)
end
As for the error code saying that h0 doesn't exist i don't really understand it as it h0 is the gui figure.

Sign in to comment.

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 3 Oct 2022
Edited: Benjamin Kraus on 3 Oct 2022
I think at a high level the issue you are having is that you are missing a signal to your script that the user has pressed the OK button. A secondary issue is that you are trying to reuse kgexec4e to both create your GUI and populate the fields (the first half of the script) and also as the callback for your button press (the switch statement), but you have no clear way to indicate to the script that it serves two purposes. There might be a third issue, in that you are trying to use global h0 but are only declaring it global in one script and not the other (this is based on your other post to MATLAB answers), but I don't recommend using global variables anyway, and you don't need them for this task.
I believe the piece you are missing is the waitfor function, coupled with some control signal that indicates your user has pressed OK.
Consider this example (which is intentionally simplied and only includes the bare minimum to demonstrate):
function [f,e,b] = createGUI()
f = figure;
e = uicontrol(f, 'Style','edit', 'Position', [20 50 60 20]);
b = uicontrol(f, 'Style','pushbutton', 'String', 'OK', 'Callback', @(o,~) set(o,'UserData',true));
end
function runExample()
% Create the GUI
[f,e,b] = createGUI;
% Pause execution, waiting for user to press OK.
waitfor(b,'UserData');
% Process data from the GUI.
disp(e.String);
% Close the GUI
close(f)
end
Here is what my example is doing:
  1. When I create the button (variable b), I'm setting the callback to be an anonymous function. This anonymous function takes as an input the button itself (variable o) and sets the UserData property on the button to true. The UserData property is normally an empty property, and it is intended to be used for storing user-defined data associated with the object.
  2. The code that runs the example (my runExample, but it is equivalent to your kgexec4e) first calls createGUI, but then it calls the waitfor function. This pauses execution of the code, waiting for some kind of signal from the GUI. In this case, it is specifically waiting for someone (anyone) to change the value of the UserData property on the button.
When the user clicks the OK button, the UserData property on the button is set to true. This change in value of the UserData property on the button is a signal to waitfor that it is time to continue executing the rest of the code in the runExample script, which then reads the user-provided string from the edit field.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2012a

Community Treasure Hunt

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

Start Hunting!