MATLAB Answers

Appdesigner: How do I include a cell-array of strings for "Items" of listbox through Appdesigner GUI?

26 views (last 30 days)
Mike Smith
Mike Smith on 11 Oct 2018
Answered: Freya H on 24 Oct 2018
I am using Matlab 2018a Appdesigner to build an App. When I initialize the App, I read-in text from an Excel file using xlsread and save it as a public variable/property. I then try to have a Listbox later in the code use this cell-array of strings to populate the list. It seems to me that the Appdesigner GUI forces you to manually enter each Item as a string and will not allow you to provide a cell-array of strings as the Items. Am I missing something here? Please see code snippets below highlighting the area I'm struggling with:
...relevant snippets from Appdesigner code...
properties (Access = public)
Listbox_RPS_Names % This is the naming list for the RPS listbox on the Geometry Tab, 2a subtab
... skipping some lines...
% Code that executes after component creation
methods (Access = private)
% Code that executes after component creation
function GUI_ReadIn_Data(app)
GUI_Data = 'GUI_Data.xlsx';
[~,~,app.Listbox_RPS_Names] = xlsread(GUI_Data,'Specification_Information','B4:Y4');
... skipping down into the "Create UIFigure and components" section of the code...
% Create ListBox_RPS
app.ListBox_RPS = uilistbox(app.SelectRPSPanel);
app.ListBox_RPS.Items = {'app.Listbox_RPS_Names'};
app.ListBox_RPS.Position = [14 12 383 310];
app.ListBox_RPS.Value = {};
The issue is that Appdesigner forces the "app.ListBox_RPS.Items" to be a string when it should be a Cell-array...!?!? Please tell me I'm doing something silly here and missing a way to feed Appdesigner a Cell-array for these listboxes, as I have many of them that I would like to edit and update from a single file as opposed to in each individual listbox assignment.
Thanks for any insight on the matter and please let me know if you need more information about what I'm asking.

Accepted Answer

Freya H
Freya H on 24 Oct 2018
One workaround would be to iterate through all entries such as:
for n = 1:size(app.Listbox_RP_names,1)
app.ListBox.Items{end+1} = app.Listbox_RP_names(n); %possibly {n} instead of (n)
This is not exactly an elegant way to do it, but that's how I managed to make it work before :)
Obviously this solution only works, if you want to add the entries to your listbox. If you want to overwrite you could either clear the listbox first or put the names into app.ListBox.Items{n}.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!