フィルターのクリア

question regarding global variables

1 回表示 (過去 30 日間)
Muazma Ali
Muazma Ali 2017 年 11 月 29 日
回答済み: Rik 2017 年 11 月 29 日
Hi!
I am wondering whether a global variable can be looked up as a local variable, meaning you calculate it in a function but not as an output variable, and then declare it as global in another function.
Thanks

回答 (1 件)

Rik
Rik 2017 年 11 月 29 日
You shouldn't use globals anyway. But yes: as long as you don't declare a variable as global, you can use it as a normal variable with that name in another function.
function foo1
global x
x=x+1;
disp(x)
end
function y=foo2
x=1:4;%not a global, just a normal variable
y=x.^2;
end
If you really must use a global, give it a very long name, which includes the function name and a description. Copy the contents to a variable with a shorter name for readability, and then copy the results back to the global.
function foo3
global x_coordinates_in_foo3__used_for_the_x_coordinate_of_something_important
x=x_coordinates_in_foo3__used_for_the_x_coordinate_of_something_important;
x=x+5;
x_coordinates_in_foo3__used_for_the_x_coordinate_of_something_important=x;
end

カテゴリ

Help Center および File ExchangeR Language についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by