listing files in list box using gui
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Pat
el 23 de Mzo. de 2012
Comentada: Image Analyst
el 24 de Dic. de 2014
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
0 comentarios
Respuesta aceptada
William
el 23 de Mzo. de 2012
Next use a listbox command
if(get(hObject, 'Value') == get(hObject, 'Max'))
filelocation = get(handles.MAT_file, 'string');
%evaluate the sting as if it were a command. See notes on
%apostrophies for this one.
loadfile =['load ''' filelocation ''''];
evalin('base',loadfile);
eval(['varlist = who(''-file'' , ''' filelocation ''');']);
listofvars = [];
for menuFillItr = 1:length(varlist)
listofvars = [listofvars varlist(menuFillItr)];
end
set(handles.listbox1,'String', listofvars);
end
This is just a snippet from some code I wrote a while ago
4 comentarios
Shanmukha priya V
el 24 de Dic. de 2014
I tried doing this.. But I'm getting an error like this: . 'The function listbox1 callback was closed with an 'end', but at least one other function definition was not. To avoid confusion when using nested functions, it is illegal to use both conventions in the same file'. Can you please tell me where exactly should I include the code which you have given? Should I include in list box call back or in the opening function?
Image Analyst
el 24 de Dic. de 2014
Either ALL your callbacks need to end with an "end" statement, or NONE OF THEM do. You can't have some end with an end and others end without an end. My style is that I don't include the final "end" on function definitions. You can use either style but you have to be consistent within an m-file.
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
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()
Ver también
Categorías
Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!