How to declare global variables in a script file which are accessible to a function file?
37 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
hello_world
le 3 Juil 2016
Réponse apportée : Walter Roberson
le 3 Juil 2016
Hello Friends,
I have a script file and a function file. My script file have a parameter which I want to be accessible to the function. For example:
%%SCRIPT FILE
I want to set global variables here, say, alpha, beta, gamma, to be accessible to myFunction
output = myFunction(x,y);
%%FUNCTION FILE
f =myFunction(U,V)
k1 = someFunction(alpha, U);
k2 = someFunction(beta, V);
k3 = someFunction(gamma, U*V');
end
How to get these globalVAriables which are alpha, beta, gamma in my SCRIPT file to myFunction?
0 commentaires
Réponse acceptée
Walter Roberson
le 3 Juil 2016
%%SCRIPT FILE
global alpha beta gamma
alpha = 57; beta = -343.8; gamma = arcsin(4323.1); %initialize them
and in the function
global alpha beta gamma
before you refer to them.
However, using global variables is not recommended. You should pass the values to the function instead.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Function Creation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!