How can I declare Hexadecimal enum constants in a classdef
6 次查看(过去 30 天)
显示 更早的评论
classdef xyz
properties(Constant)
WaveformTypes = struct('SINE',0, 'SQUARE',1, 'TRIANGLE',2, 'RAMP UP',3, ...
'RAMP DOWN',4, 'DC',5, 'PULSE',6, 'PWM',7, 'ARB',8, 'COMPOSITE',9, 'CUSTOM LUT', A);
end
methods(Static)
*functions of xyz implemented with calllib*
end
end
I tried using 0x1, 0x2, etc, but this throws a property syntax error. My first question is, will the 'A' here denote a hexadecimal value? If not, how else do I declare?
My second question, is there is a better way to implement this? Please ask if you need any further info. Cheers!
1 个评论
Stephen23
2025-7-29
编辑:Stephen23
2025-7-29
"I tried using 0x1, 0x2, etc, but this throws a property syntax error."
Writing hexadecimal and binary literals was introduced in R2019b. You are using R2017b, so I would not expect it to work.
"My first question is, will the 'A' here denote a hexadecimal value?"
No.
What is a "hexadecimal value" anyway? Any base (be it binary, decimal, or hexadecimal) are just ways of representing numbers. The underlying number is not changed by the base. So regardless what way you enter it, the stored values will be the same. You are mixing up notation with value.
"If not, how else do I declare? "
10
回答(2 个)
dpb
2025-7-29
移动:dpb
2025-7-29
To enter constants in hex base for documentation purposes (consistent with a vendor's data sheet, perhaps) with MATLAB prior to the inroduction of being able to enter them with the literal syntax, use hex2dec and, perhaps uint32
classdef WaveformType
properties(Constant)
SINE = hex2dec('0')
SQUARE = hex2dec('1')
TRIANGLE = hex2dec('2')
RAMPUP = hex2dec('3')
RAMPDOWN = hex2dec('4')
DC = hex2dec('5')
PULSE = hex2dec('6')
PWM = hex2dec('7')
ARB = hex2dec('8')
COMPOSITE= hex2dec('9')
CUSTOMLUT= hex2dec('A')
end
end
Place the above in WaveformType.m and then reference as
WaveformType.SINE
2 个评论
dpb
2025-7-29
编辑:dpb
2025-7-30
Of course, one can not use the hex representation directly to accomplish the same thing for documentation...
classdef WaveformType
properties(Constant)
SINE = 0 % 0x00
SQUARE = 1 % 0x01
TRIANGLE = 2 % 0x02
RAMPUP = 3 % 0x03
RAMPDOWN = 4 % 0x04
DC = 5 % 0x05
PULSE = 6 % 0x06
PWM = 7 % 0x07
ARB = 8 % 0x08
COMPOSITE= 9 % 0x09
CUSTOMLUT= 10 % 0x0A
end
end
I don't quite follow the other comment, but generally one can find such data in a format that can be scanned or cut 'n pasted and build the classdef (at least semi-)automagically.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!