MATLAB Answers

How to avoid Globals on a code created (complex) Figure?

2 ビュー (過去 30 日間)
Dear experts,
I am creating a GUI using pure code (without Guide). When we use Guide, we have the Handles variables that it is an input of ALL Callback functions, working similarly to a Global, since the alterations on this variable are automatically considered by other callbacks, I believe the magic is in the lines:
handles.output = hObject;
guidata(hObject, handles);
added at the end of each Callback functions that modified the handles variable. Am I right? How I use something similar with my GUI project? (I am seduced by the idea of adding the Handles as Global, but I am trying to be strong here)
I already define the callback functions on my uicontrol objects, created the functions and etc. I tried to add a created "handles" variable into the MainFigure "UserData" but this does not work since neither the Figure object is recognized by the callbacks.
THANK YOU VERY MUCH IN ADVANCE!

  1 件のコメント

Stephen Cobeldick
Stephen Cobeldick 2018 年 1 月 18 日
"I believe the magic is in the lines:"
Magic is not involved:
Reading the documentation is involved.

サインインしてコメントする。

採用された回答

Stephen Cobeldick
Stephen Cobeldick 2018 年 1 月 18 日
編集済み: Stephen Cobeldick 2018 年 1 月 18 日
One popular choice is to use guidata... however if you are writing your own GUI then I would highly recommend that you use nested functions: these make it trivially easy to pass data between functions (including callbacks):
For an example of this see my FEX submission iregexp, which uses a combination of nested and local functions.
Do NOT use globals variables or magically pass data between workspace using evalin or assignin (unless you want to force yourself into writing slow, complex buggy code that is hard to debug).

  1 件のコメント

Brunno Machado de Campos
Brunno Machado de Campos 2018 年 1 月 18 日
Thank you! In fact those lines were the answere, I just not realised that I need to use
handles = guidata(MainFig);
to retrive the data. Now Is working, thanks!

サインインしてコメントする。

その他の回答 (1 件)

Elias Gule
Elias Gule 2018 年 1 月 18 日

  0 件のコメント

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by