assigning argument to a variable

1 visualización (últimos 30 días)
Mehrdad
Mehrdad el 5 de En. de 2017
Respondida: Image Analyst el 5 de En. de 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

Respuestas (2)

Adam
Adam el 5 de En. de 2017
Editada: Adam el 5 de En. de 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 el 5 de En. de 2017

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by