assigning argument to a variable

1 次查看(过去 30 天)
Mehrdad
Mehrdad 2017-1-5
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

回答(2 个)

Adam
Adam 2017-1-5
编辑:Adam 2017-1-5
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 2017-1-5

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by