Wallem89
Wallem89

Reputation: 314

Create a mask in Simulink with edit field as enumeration

I want to create a mask on a subsystem, like the mask of the Enumerated Constant (shown below). As you see in the blue circle, the Value can be edited using a drop down list of possibilities. The Enumerated Constant

Mask of Enumerated Constant

If creating a mask it is indeed possible to make a Popup instead of an Edit but the problem with this is that the list of possible selections needs to be manually created inside the mask. What I want is that this Edit value only shows the possible selections of a certain enumeration that I want to specify only ones. The Enumerated Constant mask does this with an Edit type but even then it works.

Of course I tried to reverse engineer this from this block but I can not find how Matlab does this.

Upvotes: 2

Views: 2570

Answers (3)

Wallem89
Wallem89

Reputation: 314

I am not sure if my initial question was quite clear if I read it now. The enumerals should be taken from an enumerated type from a Data Dictionary. So based on the answers of Julian and DrBuck I could answer my own question.

I first get the enumerated type from the data dictionary.

% set name of DD and type
DDName = 'types.sldd';
EventType = 'Dem_EventIdType';
% Get DD entry
myDictionaryObj = Simulink.data.dictionary.open(DDName);
dDataSectObj = getSection(myDictionaryObj,'Design Data');
entryObj = getEntry(dDataSectObj,EventType,'DataSource',DDName);
entryValue = getValue(entryObj);

Then I get the enumerals and add the event type to it. This will be used to fill the pop-up options.

% Get enumerals
NoOfEvents = length(entryValue.Enumerals);
for i = 1:NoOfEvents
    EventIDs{i,1} = [EventType '.' entryValue.Enumerals(i).Name];
end

After this I used the above proposed code to manipulate the created pop-up menu with the enumerals from the Data Dictionary.

maskObj= Simulink.Mask.get(gcb);
par_name = 'Value_eventID'; % name
par_idx  = find(strcmp({maskObj.Parameters.Name},par_name)); % index of parameter
maskObj.Parameters(par_idx).TypeOptions = EventIDs; % enum options

I do this not in the 'Initialization Commands' but in the callback function of the 'Refresh events' button. When the block is added from the library it only contains the INVALID_EVENT. After a refresh it does a fresh look-up and adds the current enumerals to the list.

My end result:

enter image description here

Upvotes: 1

Julian Eckstein
Julian Eckstein

Reputation: 1

as DrBuck sugessted, you should use maskObj = Simulink.Mask.create(gcb) to create a mask for the currently selected block (gcb) or select an already existing mask by maskObj=Simulink.Mask.get(gcb). Then, add parameters to it:

par_name   = 'foo'; % name
par_prompt = 'This is my enum constant'; %prompt
maskObj.addParameter('Prompt',par_prompt,'Name',par_name); % add parameter

The field you are looking for is called TypeOptions, but first you must find the correct parameter number by e.g.

par_idx  = find(strcmp({maskObj.Parameters.Name},par_name)); % index of parameter

Set the mask parameter to 'popup' and create your enum values:

maskObj.Parameters(par_idx).Type = 'popup'; % type
maskObj.Parameters(par_idx).TypeOptions = {'Option1','Option2'}; % enum options

There you are ;)

HTH Julian

Upvotes: 0

DrBuck
DrBuck

Reputation: 942

I couldn't quite figure out how to do this but hopefully this answer should get you on the right track.

Create two popups on the mask, eg 'popup1' and 'popup2'. Hardcode your type options to popup1, and leave popup2 blank and disabled (because it's dependent on what you select with popup1). You can then use a callback on popup1 to populate popup2 when the first one is clicked. Your callback would look something like this:

% Grab the value selected from popup1
value = get_param(gcb, 'popup_1');

% Do some sort of check/switch statement to set your options
if value == 1 
    % Enable popup2
    set_param(gcb, 'MaskEnables', {'on', 'on'});
    % Set the type options for popup2
    set_param(gcb, 'MaskStyleString', 'popup(1|2|3|4|5),popup(my|new|options)');
end if

Have a look at this and this in the Matlab/Simulink documentation. get_param and set_param are useful if a bit unintuitive. It looks like in later versions of Simulink you can use maskObj = Simulink.Mask.create(gcb); methods to do this sort of stuff, which might be easier.

Upvotes: 0

Related Questions