Why do variables that are declared as globals get set to doubles?

3 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2009 年 6 月 27 日
The variables that are declared as globals get set to doubles. This makes a consistent programming style difficult.
a.b = 3;
c(1) = a;
% Here c is a structure, however, if you do
global d
d(1) = a;
% You get an error stating ??? Conversion to double from struct is not possible.

採用された回答

MathWorks Support Team
MathWorks Support Team 2009 年 6 月 27 日
The reason you get this error with global (or persistent) variables is that all global/persistent variables are implicitly initialized to []. Since the types need to match during subscripted assignment you get the error. In order to eliminate this error you can:
1. Avoid using the subscript (since the subscripted assignment preserves the original type)
2. Initialize d to be a struct. In this case you could use:
global d;
if isempty(d)
d = struct([]);
end
The basic idea behind suggestion 2 is that you need to create the variable to be a data type in the class of your choice. Then subscripted assignments into it will produce the results you'd like.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeIntroduction 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