Can you pass a class to a Matlab Function without calling its constructor?

I want to make a function that allows an enum class to be passed to it, so that I can display some information about that class, but I don't want the constructor of the class to be called.
Ideally, the following code would work:
function [enumDescript] = myEnumerate(enum)
enumName = inputname(1);
[enumVals, enumStr] = enumeration(enumName);
enumDescript = [num2cell(double(enumVals)) enumStr];
end
where enum is an enum such as:
classdef EnumExample < Simulink.IntEnumType
enumeration
none(0)
test(1)
fail(2)
end
end
However, when calling:
>> myEnumerate(EnumExample)
this results in the following error, beacuse the function tried to instantiate the class "enum" even though there is no need for it.
Cannot call the constructor of 'EnumExample' outside of its enumeration block.
I know I can use a string as the input of the funciton like this:
>> myEnumerate('EnumExample')
but then I can't use tab-complete, which is quite annoying for this use case.

 採用された回答

Walter Roberson
Walter Roberson 2021 年 12 月 24 日
No, you cannot do that.
However, I wonder if for your purposes it would be workable to
myEnumerate(?EnumExample)

3 件のコメント

Nice, now I can do this, which seems to work!
function [enumDescript] = myEnumerate(enum)
enumName = enum.Name;
[enumVals, enumStr] = enumeration(enumName);
enumDescript = [num2cell(double(enumVals)) enumStr];
end
And call it like this:
myEnumerate(?EnumExample)
I didn't know about the ? operator so thanks very much. If you post it as an answer I'll accept it.
Walter Roberson
Walter Roberson 2021 年 12 月 24 日
I did post it as an Answer ;-)
MrPowerElectronics
MrPowerElectronics 2021 年 12 月 24 日
Haha you're right my bad

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSimulink についてさらに検索

製品

リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by