How to get name of an enumeration in MATLAB
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I define an enumerated type in MATLAB
classdef(Enumeration) Color < Simulink.IntEnumType
enumeration
RED(0),
GREEN(1),
BLUE(2),
end
end
I can assign it:
>> x = Color.RED
x =
RED
I can display it like this:
>> disp(x)
RED
or like this
>> x.display()
x =
RED
How can I get access to that name ("RED") as a string?
In other words I'm lookin for something like:
s = x.toString()
or
s = tostring(x)
both of which do not work.
matlab oop enums
add a comment |
I define an enumerated type in MATLAB
classdef(Enumeration) Color < Simulink.IntEnumType
enumeration
RED(0),
GREEN(1),
BLUE(2),
end
end
I can assign it:
>> x = Color.RED
x =
RED
I can display it like this:
>> disp(x)
RED
or like this
>> x.display()
x =
RED
How can I get access to that name ("RED") as a string?
In other words I'm lookin for something like:
s = x.toString()
or
s = tostring(x)
both of which do not work.
matlab oop enums
add a comment |
I define an enumerated type in MATLAB
classdef(Enumeration) Color < Simulink.IntEnumType
enumeration
RED(0),
GREEN(1),
BLUE(2),
end
end
I can assign it:
>> x = Color.RED
x =
RED
I can display it like this:
>> disp(x)
RED
or like this
>> x.display()
x =
RED
How can I get access to that name ("RED") as a string?
In other words I'm lookin for something like:
s = x.toString()
or
s = tostring(x)
both of which do not work.
matlab oop enums
I define an enumerated type in MATLAB
classdef(Enumeration) Color < Simulink.IntEnumType
enumeration
RED(0),
GREEN(1),
BLUE(2),
end
end
I can assign it:
>> x = Color.RED
x =
RED
I can display it like this:
>> disp(x)
RED
or like this
>> x.display()
x =
RED
How can I get access to that name ("RED") as a string?
In other words I'm lookin for something like:
s = x.toString()
or
s = tostring(x)
both of which do not work.
matlab oop enums
matlab oop enums
edited Aug 27 '13 at 20:37
Amro
116k19210393
116k19210393
asked Feb 22 '10 at 17:33
PhilippPhilipp
2,10972853
2,10972853
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can use:
» str = char(Color.RED)
str =
RED
» class(str)
ans =
char
You can even override the default behaviour:
classdef(Enumeration) Color < int32
enumeration
RED(0)
GREEN(1)
BLUE(2)
end
methods
function s = char(obj)
s = ['Color ' num2str(obj)];
%# or use a switch statement..
end
function disp(obj)
disp( char(obj) )
end
end
end
and now:
» char(Color.BLUE)
ans =
Color 2
note: since I dont have simulink, I tested the above using the definition:classdef(Enumeration) Color < int32
– Amro
Feb 22 '10 at 18:46
Did you test whatstr
actually was? I can't test it right now, but I think this might just convert the integer representation of the enumerated type to achar
(i.e.char(0)
).
– gnovice
Feb 22 '10 at 19:41
1
@gnovice: it is returning"RED"
as expected
– Amro
Feb 22 '10 at 19:59
Ah, good. Just checking. ;)
– gnovice
Feb 22 '10 at 21:38
add a comment |
A different approach, generic, in your calling entitiy:
strtrim(matlab.unittest.diagnostics.ConstraintDiagnostic.getDisplayableString(Color.BLUE))
This way you can save the class specific implementation of disp() and/or char().
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2312774%2fhow-to-get-name-of-an-enumeration-in-matlab%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use:
» str = char(Color.RED)
str =
RED
» class(str)
ans =
char
You can even override the default behaviour:
classdef(Enumeration) Color < int32
enumeration
RED(0)
GREEN(1)
BLUE(2)
end
methods
function s = char(obj)
s = ['Color ' num2str(obj)];
%# or use a switch statement..
end
function disp(obj)
disp( char(obj) )
end
end
end
and now:
» char(Color.BLUE)
ans =
Color 2
note: since I dont have simulink, I tested the above using the definition:classdef(Enumeration) Color < int32
– Amro
Feb 22 '10 at 18:46
Did you test whatstr
actually was? I can't test it right now, but I think this might just convert the integer representation of the enumerated type to achar
(i.e.char(0)
).
– gnovice
Feb 22 '10 at 19:41
1
@gnovice: it is returning"RED"
as expected
– Amro
Feb 22 '10 at 19:59
Ah, good. Just checking. ;)
– gnovice
Feb 22 '10 at 21:38
add a comment |
You can use:
» str = char(Color.RED)
str =
RED
» class(str)
ans =
char
You can even override the default behaviour:
classdef(Enumeration) Color < int32
enumeration
RED(0)
GREEN(1)
BLUE(2)
end
methods
function s = char(obj)
s = ['Color ' num2str(obj)];
%# or use a switch statement..
end
function disp(obj)
disp( char(obj) )
end
end
end
and now:
» char(Color.BLUE)
ans =
Color 2
note: since I dont have simulink, I tested the above using the definition:classdef(Enumeration) Color < int32
– Amro
Feb 22 '10 at 18:46
Did you test whatstr
actually was? I can't test it right now, but I think this might just convert the integer representation of the enumerated type to achar
(i.e.char(0)
).
– gnovice
Feb 22 '10 at 19:41
1
@gnovice: it is returning"RED"
as expected
– Amro
Feb 22 '10 at 19:59
Ah, good. Just checking. ;)
– gnovice
Feb 22 '10 at 21:38
add a comment |
You can use:
» str = char(Color.RED)
str =
RED
» class(str)
ans =
char
You can even override the default behaviour:
classdef(Enumeration) Color < int32
enumeration
RED(0)
GREEN(1)
BLUE(2)
end
methods
function s = char(obj)
s = ['Color ' num2str(obj)];
%# or use a switch statement..
end
function disp(obj)
disp( char(obj) )
end
end
end
and now:
» char(Color.BLUE)
ans =
Color 2
You can use:
» str = char(Color.RED)
str =
RED
» class(str)
ans =
char
You can even override the default behaviour:
classdef(Enumeration) Color < int32
enumeration
RED(0)
GREEN(1)
BLUE(2)
end
methods
function s = char(obj)
s = ['Color ' num2str(obj)];
%# or use a switch statement..
end
function disp(obj)
disp( char(obj) )
end
end
end
and now:
» char(Color.BLUE)
ans =
Color 2
edited Feb 22 '10 at 19:59
answered Feb 22 '10 at 18:42
AmroAmro
116k19210393
116k19210393
note: since I dont have simulink, I tested the above using the definition:classdef(Enumeration) Color < int32
– Amro
Feb 22 '10 at 18:46
Did you test whatstr
actually was? I can't test it right now, but I think this might just convert the integer representation of the enumerated type to achar
(i.e.char(0)
).
– gnovice
Feb 22 '10 at 19:41
1
@gnovice: it is returning"RED"
as expected
– Amro
Feb 22 '10 at 19:59
Ah, good. Just checking. ;)
– gnovice
Feb 22 '10 at 21:38
add a comment |
note: since I dont have simulink, I tested the above using the definition:classdef(Enumeration) Color < int32
– Amro
Feb 22 '10 at 18:46
Did you test whatstr
actually was? I can't test it right now, but I think this might just convert the integer representation of the enumerated type to achar
(i.e.char(0)
).
– gnovice
Feb 22 '10 at 19:41
1
@gnovice: it is returning"RED"
as expected
– Amro
Feb 22 '10 at 19:59
Ah, good. Just checking. ;)
– gnovice
Feb 22 '10 at 21:38
note: since I dont have simulink, I tested the above using the definition:
classdef(Enumeration) Color < int32
– Amro
Feb 22 '10 at 18:46
note: since I dont have simulink, I tested the above using the definition:
classdef(Enumeration) Color < int32
– Amro
Feb 22 '10 at 18:46
Did you test what
str
actually was? I can't test it right now, but I think this might just convert the integer representation of the enumerated type to a char
(i.e. char(0)
).– gnovice
Feb 22 '10 at 19:41
Did you test what
str
actually was? I can't test it right now, but I think this might just convert the integer representation of the enumerated type to a char
(i.e. char(0)
).– gnovice
Feb 22 '10 at 19:41
1
1
@gnovice: it is returning
"RED"
as expected– Amro
Feb 22 '10 at 19:59
@gnovice: it is returning
"RED"
as expected– Amro
Feb 22 '10 at 19:59
Ah, good. Just checking. ;)
– gnovice
Feb 22 '10 at 21:38
Ah, good. Just checking. ;)
– gnovice
Feb 22 '10 at 21:38
add a comment |
A different approach, generic, in your calling entitiy:
strtrim(matlab.unittest.diagnostics.ConstraintDiagnostic.getDisplayableString(Color.BLUE))
This way you can save the class specific implementation of disp() and/or char().
add a comment |
A different approach, generic, in your calling entitiy:
strtrim(matlab.unittest.diagnostics.ConstraintDiagnostic.getDisplayableString(Color.BLUE))
This way you can save the class specific implementation of disp() and/or char().
add a comment |
A different approach, generic, in your calling entitiy:
strtrim(matlab.unittest.diagnostics.ConstraintDiagnostic.getDisplayableString(Color.BLUE))
This way you can save the class specific implementation of disp() and/or char().
A different approach, generic, in your calling entitiy:
strtrim(matlab.unittest.diagnostics.ConstraintDiagnostic.getDisplayableString(Color.BLUE))
This way you can save the class specific implementation of disp() and/or char().
edited Jan 3 at 9:28
answered Jan 3 at 9:07
braggPeaksbraggPeaks
591516
591516
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2312774%2fhow-to-get-name-of-an-enumeration-in-matlab%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown