Global variables inside a Matlab GUI

8 views (last 30 days)
Claudio
Claudio on 3 Jul 2014
Edited: Image Analyst on 3 Jul 2014
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

Answers (3)

Image Analyst
Image Analyst on 3 Jul 2014
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 Comments
Claudio
Claudio on 3 Jul 2014
No "clear" has been found in the code :(
Image Analyst
Image Analyst on 3 Jul 2014
Edited: Image Analyst on 3 Jul 2014
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.

Sign in to comment.


Julia
Julia on 3 Jul 2014
Edited: Julia on 3 Jul 2014
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 Comments
Julia
Julia on 3 Jul 2014
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 on 3 Jul 2014
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...

Sign in to comment.


Julia
Julia on 3 Jul 2014
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 Comment
Claudio
Claudio on 3 Jul 2014
I find this help, but it's not very clear to me. I'll try to study it better. Many thanks

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!