Clear Filters
Clear Filters

Populate drop down menus with enums

11 views (last 30 days)
Peter Foerster
Peter Foerster on 23 Nov 2018
Answered: Karl Erik Thoresen on 13 Jan 2022
Hello all,
I trying to understand enumerations and how they are to be used in matlab. I have 2 questions:
1.: In C# I can just define enums in a class:
enum someSpecificValues { one = 1, two = 2, three = 3 }:
And later use them
var enumTest = someSpeficValues.one;
Now, what I understood from the MATLAB help is that I need to define each enum as a class and then call that from within my class. I am developing a driver library, where I need a couple of settings as enums. Do I really need to define them in that way or is there a way to define them within the class? Creating them inside the class has the added advantage that they're confined to the class, where they make sense.
2.: Along with my driver I'd like to create some examples in the app creator, where I want to display these enumerated values as drop down menus, so people don't just enter random values. Is there a way to link the drop down to the enumeration or would I have to write the values by hand and hope not to make an error? Again, in C# you could bind the drop down to the enum (technically, you'd bind to the list, but the effect is the same) and changes to the enum would be reflected on your UI.
I have tried to use the DropDown.Items property and realized this accepts string arrays and not enumerations. I don't know how to correctly convert my enumeration, though. Suppose I have the class:
classdef readModes
enumeration
ZIF, SH, SHN, HDR, DD
end
end
Then the following would not work.
app.DropDown.Items = enumeration(readModes.ZIF);
But what does?
  1 Comment
Peter Foerster
Peter Foerster on 28 Nov 2018
Does no one have an idea?
I have an additional question:
How can I pass the enums to a function as an input parameter? Suppose I have the readModes from above.
In my main class I have a function:
function return = setMode (<readMode enum>)
end
How do I have to define <readMode enum> so, the function accepts an enum as the input argument and doesn't accept invalid values?

Sign in to comment.

Answers (2)

Peter Foerster
Peter Foerster on 28 Nov 2018
OK, here's what I figured out so far:
1.: Yes, I do. I still don't agree with this way of doing things.
2.: What I did now, is create some arrays to fill the drop downs from in my driver class. This is not a very elegant way, as it's not self-maintaining, but it does solve my secondary problem, where I need a subset of the enum values for another example.
Using the enumeration from above, created these arrays in my class:
modes = [readModes.SH readModes.SHN];
modeStrings = ["SH" "SHN"];
And in my app, I could populate them as:
app.DropDown.Items = app.wsaInterface.modeStrings;
app.DropDown.ItemsData = app.wsaInterface.modes;
This should do, what I demand
3.: I haven't found a way to force the passing of the correct type, but I might do something along the lines of
if class(functioParam) == class(readModes.SH)
To check if the correct parameter has been passed.

Karl Erik Thoresen
Karl Erik Thoresen on 13 Jan 2022
Try:
app.DropDown.Items = cellstr(enumeration(myClass.SomeEnum));

Categories

Find more on Software Development Tools in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!