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

조회 수: 3 (최근 30일)
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 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