define global variables in a script with functions
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponses (2)
Walter Roberson
le 10 Avr 2018
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 commentaires
Walter Roberson
le 10 Avr 2018
"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
le 24 Nov 2020
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 commentaire
Voir également
Catégories
En savoir plus sur Performance and Memory dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!