pushbutton callback doesn't work
4 views (last 30 days)
Show older comments
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
Geoff Hayes
on 26 Sep 2022
@Ali - it isn't clear to me how the code is supposed to work. For example, you have this callback (for the OK button) as
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);
which has the comment "Open GUI" which suggests that it is trying to launch the GUI. But then you mention that this callback kgexec43 is assigned to the push button in the kgui4e GUI:
uicontrol(h0,'Style','pushbutton',...
'Callback','kgexec4e',...
'Position',[250 30 65 35],...
'String','OK',...
'Tag','OptionsOK');
Is this intentional? Or is it a source of the problem?
Note that the error message is indicating that h0 does not exist (and the error is presumably coming from the above line of code). Is that the case? Can you post the full error message and perhaps your code for the GUI?
Accepted Answer
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:
- 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.
- 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.
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!