declaration of global variables

10 ビュー (過去 30 日間)
Jinho Jung
Jinho Jung 2017 年 7 月 21 日
コメント済み: Stephen23 2017 年 7 月 21 日
Hello, I think I have declared global variable correctly but it keeps saying "Error: File: Ex3_6.m Line: 10 Column: 16 The GLOBAL or PERSISTENT declaration of "n" appears in a nested function, but should be in the outermost function where it is used." I declared it in the outermost function.
Can you guys help me?
==============================================
function [avg,med]=Ex3_6(in)
global n
n=length(in);
avg=newmean(in);
med=newmedian(in);
function a=newmean(in)
global n
a=sum(in)/n;
end
function m=newmedian(v)
global n
w=sort(v);
if rem(n,2)==1
m=w((n+1)/2);
else
m=(w(n/2)+w(n/2+1))/2;
end
end
end
=================================
And I run below.
=====================================
score= score=[87 75 98 100 45 37 73];
[avg,med]=Ex3_6(score)

採用された回答

the cyclist
the cyclist 2017 年 7 月 21 日
The proper syntax is to define the subfunctions outside of the "function ... end" construct:
function [avg,med]=Ex3_6(in)
global n
n=length(in);
avg=newmean(in);
med=newmedian(in);
end
function a=newmean(in)
global n
a=sum(in)/n;
end
function m=newmedian(v)
global n
w=sort(v);
if rem(n,2)==1
m=w((n+1)/2);
else
m=(w(n/2)+w(n/2+1))/2;
end
end
  2 件のコメント
Jinho Jung
Jinho Jung 2017 年 7 月 21 日
編集済み: Jinho Jung 2017 年 7 月 21 日
I see. Thank you so much!
Another question is "can I define subfunctions inside the outermost function in any case like I did for the first time?"
I have run the function below and it works. This is why I get confused.
function z = funEx
for index=1:3
tmp=fun2;
end
z=tmp;
function y=fun2
persistent x
if isempty(x)
x=0;
end
x=x+1;
y=x;
end
end
the cyclist
the cyclist 2017 年 7 月 21 日
Actually, my brain must have been a bit switched off for the night, because I wasn't really thinking about your nested functions properly. (I almost never use nested functions.)
That being said, please do follow John's advice in his answer. (I was going to follow up with similar advice, but he beat me to it!) Not only should one avoid global variables in general, but nested functions specifically keep the parent function's variables within scope, by design.

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

その他の回答 (1 件)

John D'Errico
John D'Errico 2017 年 7 月 21 日
編集済み: John D'Errico 2017 年 7 月 21 日
I'm sorry, but this is just about the silliest reason to use a global variable I've ever seen.
n is declared as global because you don't want to pass in n as an argument to the function, perhaps because you think this is more efficient?
Worse, these are nested functions. You never needed to pass in n at all, or make it global, since they can see the variables in the workspace of the caller.
A common result of globals is buggy code. What do you have? Buggy code. Surprise.
Were you told you had to use global variables as part of this assignment? I hope not, since then your teacher would be teaching you tools you should be avoiding in the first place. Far better they should be showing you how to pass data around properly.
function [avg,med]=Ex3_6(in)
n=length(in);
avg=newmean(in);
med=newmedian(in);
function a=newmean(in)
a=sum(in)/n;
end
function m=newmedian(v)
w=sort(v);
if rem(n,2)==1
m=w((n+1)/2);
else
m=(w(n/2)+w(n/2+1))/2;
end
end
end
As you can see above, I never defined n as global, and since those are nested functions, they can see n.
Or, you could have made them subfunctions. In that case, pass in n as an argument, or as good, it hurts nothing if you just obtain the value of n in each subfunction from the vector passed in.
  1 件のコメント
Stephen23
Stephen23 2017 年 7 月 21 日
+1 correct and entertaining, as always.

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

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by