Global variables in matlab
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
kajalschopra
le 27 Juil 2015
Réponse apportée : Image Analyst
le 28 Juil 2015
I have a file_1.m file which has the following code;
global a;
a=1;
b=2;
file_2();
And my file_2.m has the following
function file_2
c=a+3;
d=2;
end
I had thought that since I have declared "a" as global in file_1.m, file_2 should have access to its value without passing a as an argument.
But it does not. How is it possible for file_2 to have access to the value of 'a' without passing it as an argument?
0 commentaires
Réponse acceptée
Walter Roberson
le 27 Juil 2015
You need to specifically say that the variable is in the global namespace in the second file.
global a
0 commentaires
Plus de réponses (1)
Image Analyst
le 28 Juil 2015
See the discussion of globals here in the FAQ. Basically it's more like friend than global , in that only functions that also declare the variable will be able to see it , and functions that don't declare the variable as global will not be able to see it.
0 commentaires
Voir également
Catégories
En savoir plus sur Variables 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!