Non-scalar enumerations of class double

1 visualizzazione (ultimi 30 giorni)
Daniel
Daniel il 19 Giu 2013
Risposto: Captain Karnage il 7 Ago 2023
Why am I not able to create an enumeration of double arrays? Consider the following simple case.
classdef Quaternion < double
enumeration
U([ 1, 0; 0, 1])
I([1i, 0; 0,-1i])
J([ 0,-1; 1, 0])
K([ 0,1i;1i, 0])
end
end
To me, everything seems in order. The enumerated values are distinct, and of the appropriate class. But when I call the constructor I get the following error.
>> u = Quaternion.
In class 'Quaternion', enumeration member named 'U' must be scalar.
I scoured the documentation, and couldn't find anything that explicitly stated this was not possible.
If this is simply not possible, perhaps Mathworks should consider making this a new feature. Seems like a no-brainer to me.

Risposte (1)

Captain Karnage
Captain Karnage il 7 Ago 2023
There's a few problems with attempting to do this in MATLAB. First, though you can make an enumeration a subclass of a built-in type - you can't do it unless your enumerations are each a specific scalar value of the superclass type. Instead, you should make it not a subclass of a built in type and instead make properties of that type, as follows:
classdef Quaternion
enumeration
U( [ 1, 0; 0, 1] )
I( [ 1i, 0; 0, -1i] )
J( [ 0, -1; 1, 0] )
K( [ 0, 1i; 1i, 0] )
end
properties
q double;
end
methods
function obj = Quaternion(q)
arguments
q (2, 2) double;
end
obj.q = q;
end
end
end
You could also make the constructor have 2 or 4 inputs, each representing either a row or individual element of your matrix, if you wanted - and could map that to a single or multiple properties as well. For example, the constructor could be obj = Quaternion(a,b,c,d) and then set obj.q = [ a, b; c, d] and validate that each input is a 1x1 scalar if you want to do validation.

Categorie

Scopri di più su Enumerations in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by