How define drop down elements based on previous input in Live Editor?
28 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Prateek Rai
2021-9-14
编辑:Prateek Rai
2021-9-15
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Guidance, Navigation, and Control (GNC) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!