How can I force Matlab to run an enum class constructor?
11 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (1)
Matt J
il 28 Feb 2020
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...
Vedere anche
Categorie
Scopri di più su Write Unit Tests in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!