How define drop down elements based on previous input in Live Editor?
30 views (last 30 days)
Show older comments
Hi,
I'm working with Live Editor trying to use Drop Down in Control elements. What I want to do is include a defined list of elements in "Engine List" if my variable Customer is "Red Bull" and a different list of elements in "Engine List" if my first input was "Ferrari or McLaren". I couldn't include my Live Script code, sorry. I'm using Matlab 2018A.
Thanks for your support
% Select your Customer in the list
%
Customer = 'Red Bull';% or 'Ferrari' or 'McLaren' in this list of elements
% Choose yor Engine
Engine = 'America';% 'Europe', 'Asia', Europe only available for "Ferrari" and Asia only for "McLaren"
0 Comments
Answers (1)
Prateek Rai
on 14 Sep 2021
Edited: Prateek Rai
on 15 Sep 2021
To my understanding, you want to create dynamic dropdown whose list items can depend on the value of other variable(or selection).
From R2021a, you can create dynamic controls in live scripts by linking variables to drop-down items. To populate the items in the drop-down list using values stored in a variable, in the Items > Variable field, select a workspace variable. The variable must be a string array to appear in the list.
So a possible workaround could be:
First Section contains:
% Select your Customer in the list
% Here "Customer" has a dropdown with 'Red Bull' , 'Ferrari', and 'McLaren' as dropdown list
Customer = 'Red Bull';% or 'Ferrari' or 'McLaren' in this list of elements
p = ["America", "Europe", "Asia"];
if strcmp(Customer,'Ferrari')
p = ["America", "Europe"];
elseif strcmp(Customer,'McLaren')
p = ["America", "Asia"];
end
%p will be string array whose value depends on the selection of Customer Dropdown
After running this section you will get the variable 'p' in your workspace.
Now, the next section should contain:
Engine = % Dropdown with 'p' as linked Variable
% Note: For linking variable 'p', Go to items --> Variable field, and select variable p
Now when you choose the' Customer' value in the dropdown, it will affect the variable 'p' and hence items in the dropdown list of 'Engine' will also change accordingly.
Please refer to Add Interactive Controls to a Live Script MathWorks documentation page to find more on adding interactive controls to a live script.
0 Comments
See Also
Categories
Find more on Guidance, Navigation, and Control (GNC) 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!