How can I read global variables?
Show older comments
Dear everyone, I used for the first time a global variable. This is my script:
emotion = ('aa_ANGER.png');
global answer; % define global variable
% Create push button with different emotions
hButton1 = uicontrol('Units','normalized','Position', UL,'Style','pushbutton','String','HAPPINESS','FontSize',16,'callback', @fCallback_F);
w = waitforbuttonpress;
if w == 0
RT = toc(tOn);
res = emotion(4:6);
nPt = emotion(1:2);
a = answer(1:3);
if strcmp(a,res)
response = 1;
else
response = 0;
end
close all
end
but in the first loop an error occurred: Index exceeds matrix dimensions Referring to a = answer(1:3); If I run the script again (without clear anything) it works. I don't know WHY!!! It something about the global variable definition but I don't know what Do i have to do.
Any help will be appreciated!
Thank you
------------------------------------------------------------ where fCallback_f is define as function elsewhere in this way:
function fCallback_F(src,evt)
global answer
answer = get(src,'String');
end
Accepted Answer
More Answers (2)
Adam
on 5 Mar 2015
To use a global variable in a function you have to call
global answer;
within that function if the global variable was created in some other function. That will create the variable within the scope of the current function allowing you to use it there.
Using global variables is almost always a bad idea and I can think of no circumstance in which it should ever be considered a good solution, but that is your decision. The above should allow you to use your global variable in any scope if you always add that line in a function in which you wish to use it.
8 Comments
Image Analyst
on 5 Mar 2015
Fed
on 5 Mar 2015
Fed
on 5 Mar 2015
Ah, ok, I didn't fully interpret all the code.
The error message suggests the problem is simply that whatever is in answer does not have 3 elements rather than that it can't access the global variable correctly.
This may be because the global variable is not behaving correctly (it is very easy to have them not behave correctly) or simply because there simply isn't a 3rd element in the variable at the point it is queried.
If I understand your code correctly you are storing the string of a pushbutton in your global variable.
Yet you have the handle for the pushbutton as hButton1 so you can simply query
get( hButton1, 'String' )
within your main code body as far as I can see, though I am not too familiar with the flow of execution where waitforbuttonpress is used.
The string on that pushbutton is not going to change though unless you are explcitly changing it via code, in which case you can access it (like above) in the same way you set it.
Fed
on 5 Mar 2015
Fed
on 5 Mar 2015
Raja Awais Liaqait
on 7 Oct 2019
0 votes
Dear All,
I want help in the following code.
global min_realvar ;
global max_realvar ;
Firstly, I want to get the value of these variables and secondly i want to write them in such away that I can give the values of these variables as an input.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!