Pushbutton callback not working

I am working on a gui which is composed of 2 listbox and 2 pushbutton ok and cancel. The first part of my programe calculate the items that will go in the listboxes. The next part allow the user when he press on ok to get the number of the selected elements in each listbox and return it in an excel file. I put an exemple of what it is supposed to do (exemple is the gui and exemple 2 is the function that run it)
My exemples work great but the actual program when it comes to the part with the switch (all the above works fine) :
function res=kgexec4e(cn,OptPref)
%kgexec4e Création de la boite de dialogue "Choix Export" associée à
%kgexec4
% Detailed explanation goes here
persistent res1 Varlist1 Varlist2%[] Initialement
%Appel du menu
h0 = kgui4e
%Calcul contenue listbox barre
Varlist1 = cn.Barres.Nom(1:end);
if OptPref.CacheBarr
Varlist1=Varlist1(~strncmp(Varlist1,'@',1)); %suppression des noms auto en fonction du menu Pref
end
%Calcul contenue listbox Element
Varlist2 = cn.Elements.Nom(1:end);
if OptPref.CacheElem
Varlist2=Varlist2(~strncmp(Varlist2,'@',1)); %suppression des noms auto en fonction du menu Pref
end
%%%Mise à jour contenu des listboxs
set(findobj(h0,'Tag','Barres'),'String',Varlist1)
set(findobj(h0,'Tag','ELement'),'String',Varlist2)
res = res1
switch get(h0,'Tag')
case 'OptOK'
nb = get(findobj(h0,'Tag','Barres'),'Value')
ne = get(findobj(h0,'Tag','ELement'),'Value')
end
end
it always gives me this error which i don't understand as the part of this programe that is slightly different from the exemple works but the similar part won't work.
here is the error message : (kguie is the gui which is the exact same as exemple gui)
Error in Caviar\private\kgui4e (line 5)
gris=[0.75 0.75 0.75];
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)
h0 = kgui4e
Error while evaluating uicontrol Callback

4 comentarios

Geoff Hayes
Geoff Hayes el 3 de Oct. de 2022
Editada: Geoff Hayes el 3 de Oct. de 2022
@Ali - I don't understand the OK pushbutton callback
uicontrol(h0,'Style','pushbutton',...
'Callback','exemple2',...
'Position',[250 30 65 35],...
'String','OK',...
'Tag','OptOK');
Why does it try to call exemple2? Haven't you already called that function to launch the GUI? Please clarify.
I also see the following error when the exemple2 code is run
SWITCH expression must be a scalar or a character vector.
Error in exemple2 (line 17)
switch get(gcbo,'Tag');
or is this one of the errors that you are referring to?
Ali
Ali el 3 de Oct. de 2022
Editada: Ali el 3 de Oct. de 2022
It might be me as i don't understand very well how to do a callback but when i have a gui and i want to call an item of this gui like a pushbutton, radiobutton or checkbox in the function that runs the gui i will use this command :
'Callback','filename',...
The exemple2 function will launch the gui and then it will set the listboxes items and then in that same function it will also when the pushbutton ok is pressed find which items in the twolistbox is pressed and return it in a excel file.
all of that is done in exemple2 that's why i have a callback for the ok button as well
And for me it runs whithout any error code
Cris LaPierre
Cris LaPierre el 3 de Oct. de 2022
Editada: Cris LaPierre el 3 de Oct. de 2022
I think there is some confusion caused by how you have chosen to name your files. My understanding is that example.m is the main file, and the one to run to launch the gui. Clicking the OK button runs the callback function, which is example2.m. This will populate the two listboxes in the gui with numbers. Is that correct?
That is the workflow I followed and did not get any errors. I selected the current folder when the popup appeared. When I select a number from either listbox, an xlsx file is added to the folder I'd selected in the popup and the app closes (file attached). The file appears to always be the same, no matter what number or listbox I select.
I am noticing line numbers in the files you have shared do not align with line numbers in the error message you pasted.
Ali
Ali el 3 de Oct. de 2022
So there is some confussion you run the exemple2.m first which launch the gui once that is done the function will populate the twolistboxes and then when you click on ok it will go through the switch statement.
The file is always the same because i didnt yet work on it as i wanted first the program to work it does now thanks to benjamin kraus answer. As for the line numbers that's an error from my part the line at which it start are not the same one is A1 and the other is A2 that's why.

Iniciar sesión para comentar.

 Respuesta aceptada

Benjamin Kraus
Benjamin Kraus el 3 de Oct. de 2022

0 votos

This looks like the same question/issue you were having in this other post. I've responded there.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2012a

Etiquetas

Preguntada:

Ali
el 3 de Oct. de 2022

Comentada:

Ali
el 3 de Oct. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by