Azzera filtri
Azzera filtri

assigning argument to a variable

3 visualizzazioni (ultimi 30 giorni)
Mehrdad
Mehrdad il 5 Gen 2017
Risposto: Image Analyst il 5 Gen 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

Risposte (2)

Adam
Adam il 5 Gen 2017
Modificato: Adam il 5 Gen 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 il 5 Gen 2017

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by