MATLAB: Listbox rendering problem 2014b

listboxpopul menu

Can anyone see whats wrong here, Im using a popup menu to select which file types to list in a list box
Warning: Single-selection 'listbox' control requires that 'Value' be an integer within String range
Control will not be rendered until all of its parameter values are valid
It works fine when I select
*.* as the filter, which is my case 1 below
On the popupmenu callback, I use
%Get user selected file type
popup_sel_index = get(handles.popupmenu1, 'Value');
switch popup_sel_index
case 1
filenameFilter = '*.*';
ImageFiles = dir(fullfile(folder,filenameFilter));
case 2
filenameFilter = '*.tif*';
ImageFiles = dir(fullfile(folder,filenameFilter)) ;
set(handles.pbDraw,'Enable','on');
case 3
filenameFilter = '*.jpg*';
ImageFiles = dir(fullfile(folder,filenameFilter));
set(handles.pbDraw,'Enable','on');
case 4
filenameFilter = '*.idat*';
ImageFiles = dir(fullfile(folder,filenameFilter));
set(handles.pbDraw,'Enable','off');
case 5
filenameFilter = '*.gtc';
ImageFiles = dir(fullfile(folder,filenameFilter));
set(handles.pbDraw,'Enable','off');
case 6
filenameFilter = '*.log*';
ImageFiles = dir(fullfile(folder,filenameFilter));
set(handles.pbDraw,'Enable','off');
case 7
filenameFilter = '*.xls*';
ImageFiles = dir(fullfile(folder,filenameFilter));
set(handles.pbDraw,'Enable','off');
set(handles.editPrefix,'String','C01-');
case 8
filenameFilter = '*red.tif*';
ImageFiles = dir(fullfile(folder,filenameFilter));
set(handles.pbDraw,'Enable','on');
case 9
filenameFilter = '*grn.tif*';
ImageFiles = dir(fullfile(folder,filenameFilter));
set(handles.pbDraw,'Enable','on');
end
guidata(hObject,handles);
drawnow;
%populate listbox with files
ListOfImageNames = {};
for Index = 1:length(ImageFiles)
baseFileName = ImageFiles(Index).name;
[folder1, name, extension] = fileparts(baseFileName);
extension = upper(extension);
ListOfImageNames = [ListOfImageNames baseFileName];
end
ListOfImageNames
set(handles.listbox1, 'string', ListOfImageNames);
drawnow;
%count files in listbox
str = get( handles.listbox1, 'String' ); % Get number of
lc = numel( str );
%disp('gtc selected true');

Best Answer

Hi,
I guess the following happened: your listbox1 contains e.g. 20 file names. You select no 15. Now using your popup menu you select another type and your listbox1 contains "only" 10 file names. But you did not change the selected no 15. Therefore: your value is outside the range 1-10.
To make the long story short: it's best practice, that when you change the 'string' property of a listbox, reset the value to the first entry, i.e.
set(handles.listbox1, 'string', ListOfImageNames, 'value', 1);
Titus
Related Question