assigning argument to a variable

조회 수: 24 (최근 30일)
Mehrdad
Mehrdad 2017년 1월 5일
답변: Image Analyst 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에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by