Borrar filtros
Borrar filtros

Using handle graphics for more global variables

2 visualizaciones (últimos 30 días)
William
William el 21 de Jun. de 2011
Can I modify the handles graphics in the GUI to store variables that I can than use globally? I need to pass variables in and out of functions in a GUI and am having problems.
Thanks

Respuesta aceptada

Matt Fig
Matt Fig el 21 de Jun. de 2011
G = guidata(gcbf); % Get the application data
G.newvariable = 5; % Store new variables in this data.
G.newvariable2 = magic(3);
guidata(gcbf,G) % Now make the new variables available to other callbacks.

Más respuestas (2)

Fangjun Jiang
Fangjun Jiang el 21 de Jun. de 2011

Eric
Eric el 21 de Jun. de 2011
handles is a structure that you can modify, as Matt points out above. His variable G is the handles structure that you're likely referencing in your question.
I prefer to use appdata rather than guidata. See the help for getappdata() and setappdata(). I think it's largely a matter of personal preference. For data that are used by most callbacks, using guidata might be more efficient from a coding point-of-view. For storing large arrays that are only used by a few callbacks, using appdata might be more memory efficient.
Regardless of which method you choose, you should make sure and document your code well. Keep a list of fields you add to the handles structure or appdata variables you define.
Good luck, Eric

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by