How to add button to mask which adds another dropdown to the same mask

6 views (last 30 days)
Hi Everyone,
I'm creating a block in simulink that will read a bunch of inputs, each of which have been mux'ed at least once, then de-mux them all and output only those selected by the user to 4 different outputs. This is so the user can isolate signals and choose what to then show on a 2x2 scope. I know this is probably possible using signal logging and all that, but I want to try to build it myself.
What I'm trying to do is create a mask for the block which gives a dropdown list of available input signals for the user to choose for each set of axes. However, since multiple signals can go on an axis, I want the user to be able to click a button which adds another dropdown (see image). Ideally, I'd also then want another button to remove a dropdown. Could anyone help me out by suggesting how I'd go about writing the callback function for each of these buttons (the "add another dropdown" and "remove dropdown" buttons).
An example of what I mean, with just the "add dropdown" button:

Accepted Answer

Uday Pradhan
Uday Pradhan on 9 Sep 2020
Edited: Uday Pradhan on 9 Sep 2020
Hi Daniel,
According to my understanding, you want to design an interactive mask dialogue box specifically adding a "popup" or "combobox" when the user clicks a control button. Also, the user should be able to remove it.
I have attached a sample model in my answer that may serve as a starting point for your design. I have only focused on the visual part of your design and not the signal selection and plotting part.
The dialog box of the masked block of this model has two different axes option, each with "Add Another" and "Remove" buttons. Clicking on "Add another" displays a "popup" bar which provides the user to select one of the signals and "Remove" removes this bar. Coming to the callbacks for these buttons, I am not "adding" any popup bar when the user clicks "Add another" button because it is illegal to add parameters from a parameter callback function, instead, I have tried to find a workaround:
As you will be able to see in the "Parameters & Dialog" section of the mask edit dialogue box, I have already created everything that is needed: All the six popup bars that can be formed are created along with the "TypeOptions" they will contain. Only catch is that they will not be visible to the user when they first try to open the masked block. To do this I have pasted the following to the LoadFcn of the masked block:
p = Simulink.Mask.get(gcb);
for i = 1:numel(p.Parameters)
a = p.Parameters(i);
a.set('Visible','off'); %all the popup bars are invisible initially
end
Now, to control the flow of "Add button" and "Remove", I define two variables "axes1Counter" and "axes2Counter" which are defined in the base workspace when the model is loaded. I have initialized them as:
axes1Counter = 0;
axes2Counter = 3;
%In your case you will need 4, since you have 4 axes and say k input signals, then
%initialize as follows : axes1Counter = 0,axes2Counter = k,axes3Counter = 2*k,axes4Counter = 3*k;
The callbacks for "Add button" for Axes 1 section is:
v = evalin('base','axes1Counter'); %axes1Counter tracks the number of times User clicks of "Add" or "remove" buttons
if v >= 3
error("Cannot add more than 3 signals!")
end
evalin('base','axes1Counter = axes1Counter + 1'); %record the click
p = Simulink.Mask.get(gcb);
a = p.Parameters(v+1); %add the required parameter i.e. popup box
a.set('Visible','on');
And the callback for "Remove" button:
%Callback for Remove (for axes 1)
v = evalin('base','axes1Counter');
if v <= 0
error("Cannot remove any signal")
end
p = Simulink.Mask.get(gcb);
a = p.Parameters(v);
a.set('Visible','off');
evalin('base','axes1Counter = axes1Counter - 1');
Keep in mind that, these callbacks will be slightly different for each axes section. For example, check the call back for "Add another" in the Axes 2 section. We have:
if v >= 6 % Observe that it is 6 and not 3
error("Cannot add more than 3 signals!")
end
This is because we are essentialy looping through the parameter set of the mask, in my model, I have 6 parameters (since I have 3 input signals and 2 axes, so for each axes the user can add upto 3 signals), so you need to set this inequality according to which axes you are adding or removing popup bars from. Feel free to play around with the model I have attached and check the callbacks used. Also, have a look at this link.
Hope this helps!
  2 Comments
Daniel Santos
Daniel Santos on 10 Sep 2020
Hi Uday, thank you so much! I will only be able to test this tomorrow, I will let you know how it works out. Thanks again!
Daniel Santos
Daniel Santos on 11 Sep 2020
Hi again Uday. Just wanted to say this is exactly what I was looking to do, thanks for the help! The link was also very useful - I knew it should be possible to do things like this, but couldn't seem to find the right documentation page about it, but there it is

Sign in to comment.

More Answers (0)

Categories

Find more on Author Block Masks in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!