Why do I get an error if I reorder the fields in a MATLAB class object?

1 回表示 (過去 30 日間)
I have created a class function named "testclass" that creates class object with differently ordered field names based on the input argument 'opt'. The code is as follows:
function cls = testclass(opt)
if(opt)
cls.a = 1;
cls.b = 2;
cls.c = 'hello';
else
cls.c = 'hello';
cls.b = 2;
cls.a = 1;
end
cls=class(cls,'testclass');
This function is placed in a folder named "@testclass".
Now, I execute the following statement:
c1 = testclass(1)
The output is given as:
c1 =
testclass object: 1-by-1
Then if I execute the following command,
c2 = testclass(0)
I receive the following error:
??? Error using ==> class
Field names and parent classes for class testclass cannot be changed
without clear classes.
Error in ==> testclass.testclass at 14
cls=class(cls,'testclass');
I did not change the field names or the number of fields in the class. All I changed was the order of the fields.

採用された回答

MathWorks Support Team
MathWorks Support Team 2009 年 6 月 27 日
The ability to change the field ordering inside a class without invoking
clear classes
is not available in MATLAB. In fact, if the order of fields is changed, MATLAB interprets that the class function definition has been changed.
To work around this issue, issue the command
clear classes
before calling the CLASS function with reordered class fields.
An example to clear classes is given below:
c1 = testclass(1);
clear classes
c2 = testclass(0);

その他の回答 (0 件)

タグ

タグが未入力です。

製品


リリース

R2006a

Community Treasure Hunt

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

Start Hunting!

Translated by