GUI is remembering values entered last time it was open, how to stop this?

4 visualizaciones (últimos 30 días)
I have edit boxes and a push-button that takes their values and runs it through a different function (Some edit boxes don't need to be filled in depending the circumstances). I had used the GUI to test it and it worked fine but after closing it and running it again with some different values, I noticed that it was retrieving data that was in the boxes the last time I ran it and is causing a disruption in the code. I even decided to make it so that if the edit box is empty to make the value equal to 0 but it did not fix the problem! It is still somehow accessing previous values entered? Any idea what might be the problem?
  4 comentarios
Stephen23
Stephen23 el 27 de Jul. de 2018
Editada: Stephen23 el 27 de Jul. de 2018
Attack of the global variables!
Do NOT use global variables. Pass data between functions as input/output arguments or nested functions. Pass data within a GUI by using guidata or nested functions.
MechE1
MechE1 el 27 de Jul. de 2018
Seeing my code not work made me cry as well haha! Thank you for your assistance!

Iniciar sesión para comentar.

Respuesta aceptada

OCDER
OCDER el 27 de Jul. de 2018
Oh... global variables are used. You should NOT pass GUI data to other functions via global variables. Otherwise your GUI will memorize previous global values until it's cleared with clear global command in the OutputFcn or a CloseRequestFcn.
Looking at your other code, the better fix is to save your "global" variables as handle variable - you almost had it right. Here's an example of your popupmenu1:
function popupmenu1_Callback(hObject, eventdata, handles)
%global n %DELETE GLOBAL !
n=(get(hObject,'Value'))-1;
handles.n=n; %GOOD! Just need to save the handles now via guidata
guidata(hObject, handles)
%To use these "global" variable in another function, do:
n_value = handles.n;
You'll have to do a lot of similar edits to your code, but it'll be much better than using global.
  8 comentarios
MechE1
MechE1 el 27 de Jul. de 2018
Editada: MechE1 el 27 de Jul. de 2018
Oh okay, sorry I'm new to GUI's I had assumed that when doing 'handles.Variable' it had to be a GUI object like a text box or button! Thank you again!
OCDER
OCDER el 27 de Jul. de 2018
The handle object is indeed a special object of matlab. Behaves almost like a structure, but not exactly the same.

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 27 de Jul. de 2018
Put this line in your OpeningFcn():
clear global;

Categorías

Más información sobre Function Creation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by