ValueIndex of ListBox can not be set to 1

6 views (last 30 days)
I created a 'Listbox' in AppDesigner
I can preset the selected items in the listbox by setting the value indices, for example
  • app.ListBox.ValueIndex = [2 3]
  • app.ListBox.ValueIndex = [3 2]
correctly preselects items with indices 2 and 3.
In addition, I can also select no item with
  • app.ListBox.ValueIndex = []
The code works for all index value combinations, as long as I do not include index value equal to 1.
In other words, I can not set index value equal to 1, i.e.
  • app.ListBox.ValueIndex = [1]
  • app.ListBox.ValueIndex = [1 2]
  • app.ListBox.ValueIndex = [1 2 3]
All give the error 'ValueIndex' must be [] or a 1-D vector of positive integers each representing an index in 'Items'.
Any idea what can be wrong here?
  5 Comments
Bas van der Vorst
Bas van der Vorst on 4 Mar 2025
Thanks for your feedback. Find some more information below.
A) Using app.ListBox.ValueIndex=1, i.e. when selecting a single item for the list the error seems to dissapear (see below). Howwver, the error remains when selecting more than one item from the list.
B) I will move to higher version to check if the error remains
C) MWE plus test results
Error message = 'ValueIndex' must be [] or a 1-D vector of positive integers each representing an index in 'Items'
I made the most simple example (only one line) to avoid any posibility of interference with other code parts but error remains
1) I made a listbox in GUI AppDesigner
2) Created listbox (default with 4 items)
3) Selected 'Multiselect' property to allow selection of more than 1 item from the list.
4) Created startupFnc with only one code line
app.ListBox.ValueIndex = [2 3 4];
This works fine.
5) I tested the following variants:
app.ListBox.ValueIndex = 1; % works fine, also for 2 3 and 4
app.ListBox.ValueIndex = [1]; % works fine, also for 2 3 and 4
app.ListBox.ValueIndex = [1 2]; % gives error
app.ListBox.ValueIndex = [1 2 3 4]; % gives error
app.ListBox.ValueIndex = [2 3 4]; % works fine
Seems selecting more than one item that includes itme with index 1 gives the error
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
ListBox matlab.ui.control.ListBox
ListBoxLabel matlab.ui.control.Label
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.ListBox.ValueIndex = [2 3 4];
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create ListBoxLabel
app.ListBoxLabel = uilabel(app.UIFigure);
app.ListBoxLabel.HorizontalAlignment = 'right';
app.ListBoxLabel.Position = [92 307 48 22];
app.ListBoxLabel.Text = 'List Box';
% Create ListBox
app.ListBox = uilistbox(app.UIFigure);
app.ListBox.Multiselect = 'on';
app.ListBox.Position = [155 257 100 74];
app.ListBox.Value = {'Item 1'};
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = app1
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
dpb
dpb on 5 Mar 2025
That is the behavior as described by @Cris LaPierre in her answer. What I didn't recognize from the description is that any occurrence of "1" in the list errors; not just if only "1" is provided.
Owing to this, my suggested workaround doesn't work if the idea is to pick item 1 along with some others.
However, it's still pretty trivial to code; just use indirect addressing...
function startupFcn(app)
iwant=[1 3]; % the list of wanted pre-selected items
app.ListBox.Value=app.ListBox.Items(iwant); % set the Value list by lookup into Items
end

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 4 Mar 2025
This appears to be a bug with ListBox in R2023b. When 'multiselect' is enabled, it throws the exception if the index is <=1 instead of <1.
This has been fixed in newer versions of MATLAB, but apparently the fix never made it back to R2023b.
The recommended workaround for now is to reference items by name instead of index. So assuming this is the listbox,
the corresponding code would be
app.ListBox.Value = {'Item 1'}
app.ListBox.Value = {'Item 1' 'Item 2'}
app.ListBox.Value = {'Item 1' 'Item 2' 'Item 3'}
I've shared a sample app.
  4 Comments
dpb
dpb on 4 Mar 2025
@Thanks, Cris...I'm holding @ R2021b at present to continue to support an app on a system controlled by an IT group so couldn't check it out.
Bas van der Vorst
Bas van der Vorst on 5 Mar 2025
Moved: Cris LaPierre on 5 Mar 2025
Dear Cris and dpb,
Thanks for your comments.
Just upgraded to MATLAB 24b and the above (very simple) code fully works now for all 'MultiSelect' combinations.
Indeed seems to be a bug in 2023b,
Kind regards,
Bas

Sign in to comment.

Categories

Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!