フィルターのクリア

How can I read global variables?

4 ビュー (過去 30 日間)
Fed
Fed 2015 年 3 月 5 日
回答済み: Raja Awais Liaqait 2019 年 10 月 7 日
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

採用された回答

David Young
David Young 2015 年 3 月 5 日
編集済み: David Young 2015 年 3 月 5 日
The problem is not connected with your use of a global variable. I think there are two possibilities:
  • If you click in the figure window, but not on the button, waitforbuttonpress will return, but nothing will have been assigned to answer . In this case, answer will be empty and you will see the error.
  • I think it is possible that waitforbuttonpress can return before the callback function has run. The following code will then get an empty value for answer and you will see the error. The next time through, answer will have received a value.
Your description and my tests indicate that it's the second of these; more time with the documentation might support the hypothesis.
Either way the solution is this. Replace the call to waitforbuttonpress with
uiwait;
Remove if w == 0 and the corresponding end .
Insert into the callback function as the final statement before end
uiresume;
Then the progress of the main script is tied in properly with the execution of the callback function.
  4 件のコメント
mat
mat 2016 年 2 月 4 日
Thanks! This solved my problem too! But what if I want to use a button (stopbutton for example) in a continuous loop where uiwait is no option?
David Young
David Young 2016 年 2 月 4 日
Then you'd need to use the button to set a variable and test its value each time round the loop. The variable could be global, but as Adam says in his answer, that's usually a bad solution, and it would be better to share it with the callback function by using nested functions.

サインインしてコメントする。

その他の回答 (2 件)

Adam
Adam 2015 年 3 月 5 日
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 件のコメント
Adam
Adam 2015 年 3 月 5 日
編集済み: Adam 2015 年 3 月 5 日
Yes, I see the problem if the same callback is assigned to all pushbuttons.
This would be a lot easier if done via GUIDE, but otherwise David Young's solution will likely allow it to carry on working with global variables.
Fed
Fed 2015 年 3 月 5 日
Next time I ll go with different solutions rather then global variables :)

サインインしてコメントする。


Raja Awais Liaqait
Raja Awais Liaqait 2019 年 10 月 7 日
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.

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by