Dynamically populating live script dropdown control

13 visualizzazioni (ultimi 30 giorni)
DNF
DNF il 21 Mar 2018
Risposto: Yash il 29 Nov 2024
I am trying the live script editor for the first time, and want to use the interactive controls. I see that I can add a dropdown menu, and manually edit it to supply a number of selections.
I would like, however, to dynamically populate the menu with selections. For example, my script might scan a directory looking for data files, and then I want to be able automatically have all those filenames loaded into the dropdown menu.
Is this possible?

Risposte (1)

Yash
Yash il 29 Nov 2024
Hi @DNF
You can use the "Variable" section to fill the list dynamically. The only catch is, you need to define the variable in the workspace first. For example, just define a one element array in the base workspace "dropdownlist". Then right click on the dropdown and select "Configure Control". Once you do that, select the "Variable" option and set it to "dropdownlist".
Now you just need to update "dropdownlist" in your code before the dropdown and it will show the list correctly. Please refer to the images below:
Before running the code: dropdownlist = [""];
After running the code:
This should work in your case.
Alternative:
If this doesn't work, you can create a uifigure and add a uidropdown to it. See the code below:
fileNames = {'data1.csv', 'data2.csv', 'experiment1.txt', 'results.xlsx', 'summary.docx'};
fig = uifigure('Name', 'Dynamic Dropdown Example');
% Create a dropdown menu and populate it with the file names
dropdown = uidropdown(fig, ...
'Items', fileNames, ...
'Position', [100, 100, 200, 22]);

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by