How can I force Matlab to run an enum class constructor?
조회 수: 7 (최근 30일)
이전 댓글 표시
I have the following enum class:
classdef MyEnum
properties
prop1
end
enumeration
A( 1, 2 )
B( 3, 4 )
end
methods( Access = public )
function this = MyEnum( x, y )
fprintf( 'Running MyEnum constructor...\n' );
this.prop1 = x + y;
end
end
end
The first time I instantiate an object of type MyEnum after opening Matlab or clearing classes, or when I make a change to MyEnum.m and save it, Matlab runs the constructor once for each enumeration:
>> foo = MyEnum.A;
Running MyEnum constructor...
Running MyEnum constructor...
>> foo = MyEnum.B;
>> clear classes
>> foo = MyEnum.B;
Running MyEnum constructor...
Running MyEnum constructor...
>>
These seem to be the only ways to run the constructor; running "clear MyEnum" and instantiating another object also doesn't run the constructor. I'd like to run it without clearing everything (as "clear classes" does). Is there a way to do this?
댓글 수: 0
답변 (1개)
Matt J
2020년 2월 28일
I'd like to run it without clearing everything (as "clear classes" does). Is there a way to do this?
You don't need to clear everything. Clearing both the class and any lingering objects of the class in the workspace would be sufficient. For example,
>> clear classes;
>> obj=myclass.A;
Running myclass constructor...
Running myclass constructor...
>> clear obj; clear myclass;
>> obj=myclass.A;
Running myclass constructor...
Running myclass constructor...
참고 항목
카테고리
Help Center 및 File Exchange에서 Argument Definitions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!