I am new to Matlab and have difficulty understanding global variables. For example
global x y z
What does global mean? Could you give me an example?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Without
global, a declared variable has a limited scope, that is, it cannot be used outside the “part” of matlab where it is declared. This wiki article has a decent discussion of what scope is in a generic sense.For Example, if you declare a variable inside a function definition, then that variable can only be used inside that function – not in another function. If you define a function at the command line (the so-called “base workspace”) it cannot be used in functions.
globaldefines a variable in the “global” scope – it can be used anywhere, in any function, etc. Except in limited cases, it is generally a bad idea, since it makes controlling how and when a variable has changed very difficult. You are usually better off returning something from a function, then passing it to another.