GLOBAL VARIABLE :HOW USE IT?

8 ビュー (過去 30 日間)
Luca Re
Luca Re 2023 年 5 月 30 日
回答済み: Image Analyst 2023 年 5 月 30 日
hi, i want to use it in a function..example:
function CaricaManageInstrument()
global dd
dd=5;
end
>> CaricaManageInstrument()
>> dd
Unrecognized function or variable 'dd'.

回答 (3 件)

Star Strider
Star Strider 2023 年 5 月 30 日
Please do not ever use global variables.
Use the approach described in Passing Extra Parameters instead.

Walter Roberson
Walter Roberson 2023 年 5 月 30 日
Global variables are only reachable within the current workspace. Consider the following code:
first(); second(); third();
a = 'set by first'
function first
global a
a = 'set by first';
end
function second
a = 'set by second';
end
function third
global a
a
end
The result is not 'set by second' because for global variables, the connection between the name and the global variable is only made in workspaces that "opt in" to the connection by explicitly declaring the variable global.
Your function CaricaManageInstrument opts in to connecting "dd" to the global variable, but once you have returned to the base workspace (the command line), the base workspace has not opted into the connection between the name and the global variable.

Image Analyst
Image Analyst 2023 年 5 月 30 日

カテゴリ

Help Center および File ExchangeScope Variables and Generate Names についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by