define global variables in a script with functions

조회 수: 4 (최근 30일)
alpedhuez
alpedhuez 2018년 4월 10일
댓글: Walter Roberson 2020년 11월 24일
I want to define a variable x at the beginning of the script and then I want a function in the script to be able to use this x without defining at the function definition.

답변 (2개)

Walter Roberson
Walter Roberson 2018년 4월 10일
That is not possible.
Functions can use variables they do not define (or "global") only if the function is a shared variable, but scripts cannot establish shared variables for functions defined in the same file as the script.
  댓글 수: 2
alpedhuez
alpedhuez 2018년 4월 10일
why?
Walter Roberson
Walter Roberson 2018년 4월 10일
"Because".
Suppose what you asked for were possible. Then that would mean that something outside a routine could "inject" variables into a function that is not expecting them to be from an outside source. That could potentially completely change the meaning of the function.
Consider for example,
%script
sum = @prod;
result1 = testfun(1:10);
sum = @sqrt;
result2 = testfun(1:10);
function y = testfun(x)
y = sum(x);
end
Now, what is that to mean in testfun? Should it be looking inside the script in case the function sum has been defined in its execution environment, and then end up calculating factorial(10) in the first call and the square roots in the second call? If so then that would contradict everything else about the MATLAB search path, which for functions or for code invoked at the command line does not search the environment of the caller to look for potential definition for a variable.
The behaviour is consistent for scripts and functions and command lines: in each case MATLAB would not even look in the calling environment and would take sum from the regular path resolution, probably using the built-in method toolbox/matlab/datafun/sum
You could speculate that perhaps there could be something like
%script
inject('testfun', 'sum', @prod) %does not really exist
result1 = testfun(1:10);
inject('testfun','sum', @sqrt) %does not really exist
result2 = testfun(1:10);
function y = testfun(x)
y = sum(x);
end
where the intention would be that the calling routine is ordering that a called function not use the regular name resolution and instead resolve 'sum' to the given value. No such facility exists -- and if it existed it would make writing secure code quite difficult. For example, if the facility existed then a caller could command
inject('password_check', 'strcmp', @(A,B) true)
to tell password_check that strcmp is always to return true. This Is Not Good.
What MATLAB does have is a facility for sharing variables where the sharing is built right into the syntax, where the function using the variable was designed right into the first function, such as
function result1 = outer
sum = @prod;
result1 = testfun(1:10);
function y = testfun(x)
y = sum(x);
end
end
This is still deceptive to the casual reader, but it is a private function which does not exist outside of the context of the outer function -- it is not being forced to do anything it does not expect, it is only cooperating with the surrounding routine.
This facility to share variables does not exist between a script and a function defined in the script: it only exists in the nesting functions situation.

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


SAyed Amid
SAyed Amid 2020년 11월 24일
Create a function called problem2 that compute the following expression:
-x3 – 4x – 9 + e^x
• Use the fplot function to create a plot from x = -7 to x = +7
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 11월 24일
This does not appear to be an Answer to the question that was asked.

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

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by