listing files in list box using gui
조회 수: 5 (최근 30일)
이전 댓글 표시
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
채택된 답변
William
2012년 3월 23일
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
Shanmukha priya V
2014년 12월 24일
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
2014년 12월 24일
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.
추가 답변 (2개)
William
2012년 3월 23일
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
2012년 3월 23일
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()
참고 항목
카테고리
Help Center 및 File Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!