GUI handle empty after using some scripts
3 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Jan
on 22 Nov 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 Comments
Jan
on 22 Nov 2022
Edited: Jan
on 22 Nov 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.
More Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!