declare variable accross all button

hello guys, i have problem in declaring a variable, im building a GUI(guide user interface) with some buttons, what i mean is to declare variable "k=1" globally so every buttons "function pushbutton_Callback(hObject, eventdata, handles)" can use it in the same mean, because every time i push other buttons it says "??? Undefined function or variable 'k' " . if u guys have exprience in building gui, where should i put the variable at? or maybe using particular function?

 Respuesta aceptada

Image Analyst
Image Analyst el 29 de Oct. de 2011

0 votos

The above two options, plus some additional ones, are described in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.3F

6 comentarios

Image Analyst
Image Analyst el 29 de Oct. de 2011
Actually it looks like someone removed the global option. It seems the global-haters are about as fervent as the eval-haters.
Izzo
Izzo el 30 de Oct. de 2011
for the option "handles.myNewVariable = 42;" where shall i put it?
Walter Roberson
Walter Roberson el 30 de Oct. de 2011
Izzo, please read through http://www.mathworks.com/matlabcentral/answers/19529-how-to-pass-arrays-to-and-from-a-gui
Izzo
Izzo el 30 de Oct. de 2011
great it's almost working, i declared it on "function viewim_OpeningFcn(hObject, eventdata, handles, varargin)"
i declared "handles.k = 1;"
but whenever i click button in run (handles.k=handles.k+1), i think the handles.k values still remains "handles.k = 1" after the button committed, any ideas?
Walter Roberson
Walter Roberson el 30 de Oct. de 2011
Do you have
guidata(hObject,handles)
after you do the assignment?
Izzo
Izzo el 30 de Oct. de 2011
yes, the "guidata(hObject,handles)" it's at the end of function "function viewim_OpeningFcn(hObject, eventdata, handles, varargin)" .

Iniciar sesión para comentar.

Más respuestas (2)

Walter Roberson
Walter Roberson el 29 de Oct. de 2011
It is not possible to do that in MATLAB, and it would be a disaster if you could, since there are no doubt plenty of MATLAB library routines that use "k" for their own purposes.
Perhaps reading the documentation for global would help clear this up:
Ordinarily, each MATLAB function has its own local variables, which are separate from those of other functions, and from those of the base workspace. However, if several functions, and possibly the base workspace, all declare a particular name as global, they all share a single copy of that variable. Any assignment to that variable, in any function, is available to all the functions declaring it global.
Notice how the value is only shared with "all the functions declaring it global", not with all routines.
Naz
Naz el 29 de Oct. de 2011
Just simply use handles.k instead of k everywhere. Then, after you changed this variable and want to save changes to it, you need to type
guidata(hObject, handles); % this saves changes

Categorías

Más información sobre Entering Commands en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 29 de Oct. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by