GUI handle empty after using some scripts

3 次查看(过去 30 天)
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.

采纳的回答

Jan
Jan 2022-11-22
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 个评论
Ali
Ali 2022-11-22
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 2022-11-22
编辑:Jan 2022-11-22
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by