listing files in list box using gui
Mostrar comentarios más antiguos
i have created gui with list box and 1 pushbuttons,now if i click that push button the name of files in my folder must be displayed in listbox(i have 10images in a folder in D drive),ten if i double click the name of file in file selector ,the image should be displayed,please help hoe to do
Respuesta aceptada
Más respuestas (2)
William
el 23 de Mzo. de 2012
first use the Uigetfile
if(get(hObject, 'Value') == get(hObject, 'Max'))
[MATfilename, MATpath, filterindex] = uigetfile('*.MAT','Pick .MAT file')
end
%update the GUI
filelocation = strcat(MATpath, MATfilename)
set(handles.MAT_file, 'string', filelocation);
guidata(hObject, handles); % Update Figure
1 comentario
Pat
el 24 de Mzo. de 2012
Image Analyst
el 23 de Mzo. de 2012
But otherwise, here's a snippet of code I use called LoadImageList(). You can call it when you start up your code (in the OpenFcn function), or, for example, in the callback for the "Specify folder" button where the user browses to the folder they want (using uigetdir or uipickfiles). Don't be afraid of the length. It's just a little long because it's so well commented and robust. I'm sure once you read through it line by line you can understand it. You could make it shorter if you wanted by concatenating a bunch or dir() outputs if you want rather than by filtering the files by extension.
%=====================================================================
% Load up the listbox, lstImageList, with image files
% in the folder handles.CalibrationFolder.
function handles = LoadImageList(handles)
try
% Get the folder into a handy variable name.
% (Change handles.CalibrationFolder as needed for your situation.)
folder = handles.CalibrationFolder;
% Check that the folder actually exists.
if ~isempty(folder)
% Folder is not empty. There is actually some text in it.
if ~exist(folder,'dir')
% Folder does not exist on disk.
WarnUser(['Folder ' folder ' does not exist.']);
return;
end
else
% Folder is empty/null.
WarnUser('ERROR: No folder specified as input for function LoadImageList.');
return;
end
% If it gets to here, the folder exists.
% Get a list of all files in the folder.
filePattern = fullfile(folder, '/*.*');
ImageFiles = dir(filePattern);
% Filter the list to pick out only files of
% file types that we want (image and video files).
ListOfImageNames = {}; % Initialize
for Index = 1:length(ImageFiles)
% Get the base filename and extension.
baseFileName = ImageFiles(Index).name;
[folder, name, extension] = fileparts(baseFileName);
% Examine extensions for ones we want.
extension = upper(extension);
switch lower(extension)
case {'.png', '.bmp', '.jpg', '.tif', '.avi'}
% Keep only PNG, BMP, JPG, TIF, or AVI image files.
ListOfImageNames = [ListOfImageNames baseFileName];
% otherwise
end
end
% Now we have a list of validated filenames that we want.
% Send the list of validated filenames to the listbox.
set(handles.lstImageList, 'string', ListOfImageNames);
catch ME
% Alert the user of the error.
errorMessage = sprintf('Error in LoadImageList().\nThe error reported by MATLAB is:\n\n%s', ME.message);
WarnUser(errorMessage); % WarnUser code is uiwait(warndlg(errorMessage));
% Print the error message out to a static text on the GUI.
set(handles.txtInfo, 'String', errorMessage);
end
return; % from LoadImageList()
%--------------------------------
function WarnUser(warningMessage)
uiwait(warndlg(warningMessage));
return; % from WarnUser()
1 comentario
Pat
el 24 de Mzo. de 2012
Categorías
Más información sobre Scripts en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!