Clear Filters
Clear Filters

assigning argument to a variable

1 view (last 30 days)
Mehrdad
Mehrdad on 5 Jan 2017
Answered: Image Analyst on 5 Jan 2017
hi guys
I have a question about the following commands. They are quite simple but I want to know what is going on behind. I wrote;
global interpmethod gridmethod
(by above global statement MatLab initializes an empty 0x0 matrix to the variable. right?)
What are the followings do? we are equalizing the variables to an argument (like linear).In particular, what will be changed to the mentioned 0x0 matices? and are such arguments like 'equalsteps' and 'linear' defined across all commands in the matlab?
gridmethod= 'equalsteps';
interpmethod = 'linear';
thanks

Answers (2)

Adam
Adam on 5 Jan 2017
Edited: Adam on 5 Jan 2017
global interpmethod gridmethod
defines to empty global variables of those names or, if they already exist in the global namespace they will be parachuted into the current workspace with whatever values they currently have. If they already exist in the current workspace you will sometimes get a warning, but the existing variables in the workspace will now be made global also.
gridmethod= 'equalsteps';
interpmethod = 'linear';
just assign chars to the two variables. Then if you again call
global interpmethod gridmethod
in some other workspace later on (assuming you haven't cleared the global variables) then they will appear in that workspace with the same strings.
The use of global variables is awful program design though that can lead to numerous obscure bugs. There is almost never (or literally never) a need to use them in proper code.
Workspaces exist for a reason and variables are assigned to a single workspace by default for a very good reason too. If you want them in another workspace pass them as a function argument.

Image Analyst
Image Analyst on 5 Jan 2017

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!