Global variables inside a Matlab GUI

5 次查看(过去 30 天)
Dear all,
I have a Matlab GUI with several callback functions. I need to pass a variable of type struct from one callback function to another.
I therefore inizialized a global variable "config" in the very first function of the .m file.
Then, in the callback functions I implemented the following code:
% Callback #1
function clbk1_Callback(hObject, eventdata, handles)
global config
config.clbk1=get(hObject,'String');
% Callback #2
function clbk2_Callback(hObject, eventdata, handles)
global config
config.clbk2=get(hObject,'String');
Etc.
The problem is that "config" seems to be initialized in every callback function. That is to say:
1) I call the 1st callback function, "config" is:
config.clbk1='mickey'
2) After the 1st, I call the 2nd callback function, "config" should be
config.clbk1='mickey'
config.clbk2='mouse'
Instead, it's only
config.clbk2='mouse'
What I'm missing?
Many thanks to anyone could help, Claudio
P.s.: Matlab version 2010b, Windows 8.1, both 64 bit

回答(3 个)

Image Analyst
Image Analyst 2014-7-3
You probably have a
clear global;
or
clear global config;
or
config = [];
somewhere in the code. I don't see any other way that it could be cleared, except "clear all". You don't have a clear all anywhere do you? Search your code for the word "clear" and tell me what you find.
  2 个评论
Claudio
Claudio 2014-7-3
No "clear" has been found in the code :(
Image Analyst
Image Analyst 2014-7-3
编辑:Image Analyst 2014-7-3
What Julia said about "global config" initializing the variable is not true. If config already has a value, then as soon as you execute that global line, you will now see the variable in your function and the config value will have all value(s) that it had prior to that point. It will not be wiped clean.
Make a small script demonstrating the problem and I'll look at it. For example, put this code in test3.m and run it.
function test3
clc;
global g
g = pi; % Assign a value.
% Call f1
fprintf('Prior to calling f1, g = %f\n', g);
f1
fprintf('After calling f1, g = %f\n', g);
function f1
global g;
fprintf('In f1, g = %f\n', g);
g = 42.5;
fprintf('In f1, g = %f\n', g);
Or maybe copy your program and start hacking out unrelated code that has nothing to do with config and see if it ever starts working. I still think you're either not assigning it in the first place, assigning it to null, or calling clear global. You can attach your code if you want but make sure it can run, i.e. attach any data files it might need, etc.

请先登录,再进行评论。


Julia
Julia 2014-7-3
编辑:Julia 2014-7-3
I am not sure if you really need a global variable to do what you want. You can simply define a new variable in the opening function:
handles.test=hObject;
guidata(hObject, handles); % updates handles structure
and configure it in the callbacks:
handles.test = ...
guidata(hObject, handles); % updates handles structure
If you have to use a global, I still think you have to update the handles structure after assigning a value.
  3 个评论
Julia
Julia 2014-7-3
Your test probably works because you don't do anything in the first callback. In the second you use the handles structure to get the value from the 1st callback. (that should always work)
I do not often use global variables so I don't know how they are initialized correctly. Could it be that you initialize config each time you write
global config
?
Claudio
Claudio 2014-7-3
Quote: "Your test probably works because you don't do anything in the first callback. In the second you use the handles structure to get the value from the 1st callback. (that should always work)"
It happens also if I make something inside the 1st callback function :(
Quote: "I do not often use global variables so I don't know how they are initialized correctly. Could it be that you initialize config each time you write..."
It seems so, but I should not be, according to the Matlab help...

请先登录,再进行评论。


Julia
Julia 2014-7-3
Found this in the Matlab help:
To use a global within a callback, declare the global, use it, then clear the global link from the workspace. This avoids declaring the global after it has been referenced. For example,
cbstr = sprintf('%s, %s, %s, %s, %s', ...
'global MY_GLOBAL', ...
'MY_GLOBAL = 100', ...
'disp(MY_GLOBAL)', ...
'MY_GLOBAL = MY_GLOBAL+1', ...
'clear MY_GLOBAL');
uicontrol('style', 'pushbutton', 'CallBack', cbstr, ...
'string', 'count')
  1 个评论
Claudio
Claudio 2014-7-3
I find this help, but it's not very clear to me. I'll try to study it better. Many thanks

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by