我想你知道所有文件夹所在的基本路径 . 然后你可以使用这样的东西:
% First find all folders
folders = cell(0); % empty cell to save folder names
nFolders = 0;
allFolders = ls(basePath); % find all files and folders
for k=1:size(allFolders,1)
curFolder = fullfile(basePath,strtrim(allFolders(k,:)));
if isdir(curFolder) % find out if it is a folder
if ~(allFolders(k,1) == '.') % ignore '.' and '..'
folders{nFolders+1,1} = curFolder; % Save folder path
nFolders = nFolders + 1;
end
end
end
% Then find all files inside these folders
files = cell(0); % empty cell array for file names
nFiles = 0;
for k=1:nFolders % go through all folders
allFiles = ls(folders{k,1});
for l=1:size(allFiles,1) % go through all found files/subfolders
curFile = fullfile(folders{k},strtrim(allFiles(l,:)));
if ~isdir(curFile) % only select files
files{nFiles+1,1} = curFile; % and save it to the cell
nFiles = nFiles + 1;
end
end
end
现在,您可以遍历 files 单元格并根据您的脚本读取所有文件 . 我看到你对文件名感兴趣 . 您可以通过提取文件名
[path,filename,extension] = fileparts(files{k,1});
要导入文本文件,您可以使用 dlmread ,我认为它比 textscan 更直观(但当然有更多限制) . 为此,您不必使用 fopen 打开文件,您可以直接提供文件名 .
value = dlmread(fileName,' ',[23,13,23,13]);
分隔符现在是一个空格,只读取row = 23 / col = 13的值 . 请注意,范围从row / col = 0开始,而不是像Matlab中通常那样1 - 所以也许你必须将它改为 [22,12,22,12] .