GUI handle empty after using some scripts

3 visualizaciones (últimos 30 días)
Ali
Ali el 22 de Nov. de 2022
Editada: Jan el 22 de Nov. de 2022
Hello,
I have a gui that i am using it depends on a lot of .m files and works great but sometimes when a random script is running at the same time it will make the gui unusable (i can access it's menus but clicking on them will do nothing) and force me to use the close all force command to close it. I searched a bit and i found that the reason why it does that the gui handles is empty it can't find the figure while it is still open :
here the gui handle is kg4_HndlFig and when i tried to close the gui without another script running it finds the figure but when i have another script running it's empty.
If anyone had this kind of problem or knows how to resolve this it would be helpfull.

Respuesta aceptada

Jan
Jan el 22 de Nov. de 2022
Running a pile of scripts impedes the debugging massively, as you can see. Any of the scripts can be responsible for overwriting the handle of the GUI. Therefore productive code should avoid scripts in general, but use functions. Functions have their own workspace and the values of the locally used variables cannot be overwritten by any code outside.
The callbacks of a GUI should use the handle stored in the function itself. There should not be any dependency to variables in the base workspace to avoid exactly such error as your current one.
The oberserved error is an effect of the programming style. Sorry to this pessimistic opinion, but the reliable solution is to re-write the code from scratch avoiding scripts completely.
  2 comentarios
Ali
Ali el 22 de Nov. de 2022
Thanks for the advice.
I found out what the caused this it's because there is a clear all in the other script but is there a way to keep the gui variables even if a clear all command is used ? Is it possible to clear the matlab workspace without clearing the workspace of the gui ?
Jan
Jan el 22 de Nov. de 2022
Editada: Jan el 22 de Nov. de 2022
Clearing the base workspace does not clear the variables stored in the ApplicationData and UserData of a GUI. So the general rules are:
  • Store data and handles in the figure's ApplicationData oder UserData (see e.g. guidata)
  • Do not use scripts, but functions.
  • Avoid clear all , because this is a massive waste of time without benefits. It might be useful for a small hack written in a script, but it should not appear in productive code. Beside deleting the variables of the local workspace, it removes all loaded functions from the memory. Reloading them from the slow disk and parsing them takes precious time and power. clear all is "cargo cult programming". It has helped under very specific circumstances and it is inserted afterwards in all code as voodoo.
  • Do not use global variables, because they suffer from the same problems as storing variables in the base workspace or using scripts. In your GUI the variables "language" and "first_time" are concerned.
Your code has a design called "programmers hell". As said already, I recommend to rewrite it from scratch and to remove the mentioned standard pitfalls. Otherwise the complexity might explode if you expand the code further. I've seen too many projects failing due to such mistakes.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by