- 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.
pushbutton callback doesn't work
7 views (last 30 days)
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 :
%kgexec4e Création de la boite de dialogue "Choix Export" associée à
% Detailed explanation goes here
persistent res1 Varlist1 Varlist2 % Initialement
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
res = res1
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
The gui is set in another .m file called kgui4e.m and i set the pushbutton this way
'Position',[250 30 65 35],...
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
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
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));
% Create the GUI
[f,e,b] = createGUI;
% Pause execution, waiting for user to press OK.
% Process data from the GUI.
% Close the GUI
Here is what my example is doing:
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.