필터 지우기
필터 지우기

List files within a folder

조회 수: 11 (최근 30일)
Inês Mendes
Inês Mendes 2015년 6월 4일
댓글: Image Analyst 2015년 6월 4일
Hi guys,
I am trying to list all the files i have inside a folder but i am having some difficulties. First of all, i listed the names of all the folders i have inside that directory. Now, i am trying to list the files within those folders. I used the code bellow but it´s not working :X
handles.diretorio='H:\Tese\ferramenta\excel\';
handles.lista=dir(fullfile(handles.diretorio));
handles.C=get(handles.popupmenu5,'String');
handles.C=handles.lista;
handles.listaC=dir(fullfile(strcat(handles.diretorio,handles.C)));
Can anyone help?
Inês
  댓글 수: 1
Sean de Wolski
Sean de Wolski 2015년 6월 4일
Define "it's not working"

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2015년 6월 4일
You can use genpath(). That's what I do in my attached demo. It will recurse into all subfolders and find the names of all the files in all those folders.
  댓글 수: 2
Inês Mendes
Inês Mendes 2015년 6월 4일
can you give an example? i am not being able to use it!
i would be much appreciated!
Image Analyst
Image Analyst 2015년 6월 4일
Try this code. I changed it a bit and it's attached again.
% Start with a folder and get a list of all subfolders.
% Finds and prints names of all files in
% that folder and all of its subfolders.
% Similar to imageSet() function in the Computer Vision System Toolbox: http://www.mathworks.com/help/vision/ref/imageset-class.html
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Define a starting folder.
start_path = fullfile(matlabroot, '\toolbox');
if ~exist(start_path, 'dir')
start_path = matlabroot;
end
% Ask user to confirm or change.
uiwait(msgbox('Pick a starting folder on the next window that will come up.'));
topLevelFolder = uigetdir(start_path);
if topLevelFolder == 0
return;
end
% Get list of all subfolders.
allSubFolders = genpath(topLevelFolder);
% Parse into a cell array.
remain = allSubFolders;
listOfFolderNames = {};
while true
[singleSubFolder, remain] = strtok(remain, ';');
if isempty(singleSubFolder)
break;
end
listOfFolderNames = [listOfFolderNames singleSubFolder];
end
numberOfFolders = length(listOfFolderNames)
% Process all image files in those folders.
for k = 1 : numberOfFolders
% Get this folder and print it out.
thisFolder = listOfFolderNames{k};
fprintf('Processing folder %s\n', thisFolder);
% Get ALL files.
filePattern = sprintf('%s/*.*', thisFolder);
baseFileNames = dir(filePattern);
% % Get m files.
% filePattern = sprintf('%s/*.m', thisFolder);
% baseFileNames = dir(filePattern);
% % Add on FIG files.
% filePattern = sprintf('%s/*.fig', thisFolder);
% baseFileNames = [baseFileNames; dir(filePattern)];
% Now we have a list of all files in this folder.
numberOfImageFiles = length(baseFileNames);
if numberOfImageFiles >= 1
% Go through all those files.
for f = 1 : numberOfImageFiles
fullFileName = fullfile(thisFolder, baseFileNames(f).name);
fprintf(' Processing file %s\n', fullFileName);
end
else
fprintf(' Folder %s has no files in it.\n', thisFolder);
end
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Audio and Video Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by