How can I use attribute GLOBAL efficiently?

MATLAB Help describes this topic (word "global") very shortly.
I ask it from such question:
M-file as function:
function [y]=y(x) a=2; y=a*x.^2 end
how to force the procedure to modify global variables? that is, after proc. work, some results will be saved in globals and be available for other procedures.
For example, assigning a=2 I wish to keep. But I don't want use simple ways: 1) text file 2) write [y,a] in header, or y=[y; a] in body
If I write global a; a=2
this haven't effect...
and may anybody give good practical code example with and without GLOBAL?

 채택된 답변

Jan
Jan 2011년 3월 31일

2 개 추천

Working with GLOBALs is fragile in all programming languages: It is very hard to find out, where and why the value was set the last time.
Therefore I'd use Paulo's approach ever: "function [y,a]=y(x)" and forward of [a] to all functions, which need it.
Another approach is using a dedicated function to store the value persistently:
function a = Store(Command, a)
persistent a_
switch Command
case 'get', a = a_;
case 'set', a_ = a;
otherwise error('unknown command');
end
Although this has the same drawback as GLOBAL ([a] can be influenced from anywhere), you can at least use the debugger to track changes and usage of [a].
But it is at least possible to use GLOBALs (I avoid using "y" as variable and function name at the same time):
function yValue = y(x)
global a
a = 2;
yValue = a * x .^ 2;
end
Then from the command line or inside another function:
global a
disp(a) % >> nothing
k = y(1:10);
disp(a) % >> 2

댓글 수: 4

Igor
Igor 2011년 4월 1일
This effects :)
But I have a question -- what is sense of double using global?
GLOBAL within function -- as I know, directive to parser to use global var (no create new local var.)
GLOBAL out function -- ?? Any variable emerged from command line have automatically grobal scope, haven't it?
David Young
David Young 2011년 4월 1일
Igor: you have to declare the variable as global in every workspace in which it is used, including the command-line workspace. Variables in the command-line workspace are not global unless you declare them to be. (You can see this with a simple experiment: make a function that declares a global and prints its value, give a variable with the same name a value at the command line and call your function.)
Jan
Jan 2011년 4월 1일
@Igor: Imagine declaring a variable once as GLOBAL would be enough. Then I insert "global sin; sin=0;" in any M-file. Afterwards no Matlab function would be able to calculate a SIN anymore, because the variable would shadow the function SIN. The same problem would appear for all variables: if "y" is such a brute GLOBAL concerning all functions, any function using this would overwrite the value - e.g. Matlab's MEAN function!
Igor
Igor 2011년 4월 1일
i suppose that at restricted sphere it's a plus!
but GLOBAL var. must be unique named
it's needed more believe in programmer!?

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Paulo Silva
Paulo Silva 2011년 3월 31일

0 개 추천

The way you should always work with is:
function [y,a]=y(x)
if you still want to use global variables you must declare the variable as global everywhere you want to use it (other functions and workspace)

댓글 수: 9

Igor
Igor 2011년 3월 31일
I still want with global :)
globalexample
function [y]=y(x)
global a
a=3;
y=a*x.^2
end
----
>>a=5;globalexample(4), a
No effect from GLOBAL
Paulo Silva
Paulo Silva 2011년 3월 31일
you have to declare global a in the workspace before your code!
global a;a=5;globalexample(4), a
Igor
Igor 2011년 3월 31일
NO EFFECT
globalexample.m --
function [y]=y(x)
a=3;
y=a*x.^2
end
>> global a;a=5;globalexample(4), a
=
48
ans =
48
a =
5
Paulo Silva
Paulo Silva 2011년 3월 31일
NO EFFECT BECAUSE YOU DON'T READ, I SAID "if you still want to use global variables you must declare the variable as global everywhere you want to use it (other functions and workspace)"
YOU REMOVED global a FROM YOUR FUNCTION AND PUT IT IN THE WORKSPACE
Jan
Jan 2011년 3월 31일
@Paulo: You are right. Igor removed the GLOBAL statement from the function, which is a mistake. Such mistakes happen, especially if the author is not familiar with this method at all. I think, we have helped him to learn more.
Paulo Silva
Paulo Silva 2011년 3월 31일
I said it in just a few words what he should do and he ignored it, I should have used bold letters, bigger font or whatever. Mistakes happen but those darn NO EFFECTS shouldn't happen.
??? Error using ==> The Computer
Please replace the user of this computer and try again
Jan
Jan 2011년 3월 31일
@Paulo: Let me cite Matt Fig: "... the rest is just fluff".
Igor might be 8 or 88 years old, working with Matlab since 6 hours only, very tired or confused by the the impressive power of Matlab. Anyway, I do not see a reason to be inpolite.
Paulo Silva
Paulo Silva 2011년 3월 31일
Paulo==BeingImpolite
ans =
1
Igor
Igor 2011년 4월 1일
jokers...
MATLAB is a powerful THING!

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2011년 3월 31일

0 개 추천

Do not use a variable name which is the same as the name of your function: doing so may lead to unwanted recursion.
David Young
David Young 2011년 3월 31일

0 개 추천

If you find a real need to use global variables to share data between functions, consider adopting an object-based approach instead. The functions that need to share become methods in a class, and the shared variables become properties of that class.

댓글 수: 2

Igor
Igor 2011년 4월 1일
From theoretical aspect, you are right.
As I suppose, using GLOBAL instead of object incapsulation is like as a using GOTO for procedure oriented programmers.. :)
Jan
Jan 2011년 4월 1일
@Igor: During debugging, GLOBAL is even more a COMEFROM than a GOTO.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

질문:

2011년 3월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by