How define drop down elements based on previous input in Live Editor?

조회 수: 34 (최근 30일)
Luis Lopez
Luis Lopez 2021년 7월 21일
편집: Prateek Rai 2021년 9월 15일
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"

답변 (1개)

Prateek Rai
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.

카테고리

Help CenterFile Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by