Matlab 从多个文件夹加载多个文本文件
我正忙于一项研究,我要用Matlab来研究数据。 但是我在将所有文本文件加载到Matlab中时遇到了麻烦,因为我无法获得非常长的代码 设置如下所示:Matlab 从多个文件夹加载多个文本文件,matlab,Matlab,我正忙于一项研究,我要用Matlab来研究数据。 但是我在将所有文本文件加载到Matlab中时遇到了麻烦,因为我无法获得非常长的代码 设置如下所示: Testsubject1/ts1_bla.txt Testsubject2/ts2_bla.txt 我自己也用荷兰语来表达,但我猜例程与bla部分中的几个文件是一样的 我可以将此代码用于1个目录,但每次都必须切换目录以加载数据: files = dir('.\Proefpersoon1\*.txt'); for i=1:length(files)
Testsubject1/ts1_bla.txt
Testsubject2/ts2_bla.txt
我自己也用荷兰语来表达,但我猜例程与bla
部分中的几个文件是一样的
我可以将此代码用于1个目录,但每次都必须切换目录以加载数据:
files = dir('.\Proefpersoon1\*.txt');
for i=1:length(files)
eval(['load ' files(i).name ' -ascii']);
end
我也不介意将它们保留为数组,但是有没有办法使用它进行循环
files = dir('.\Proefpersoon1\*.txt');
副本:
功能如下:
function fileList = getAllFiles(dirName)
dirData = dir(dirName); %# Get the data for the current directory
dirIndex = [dirData.isdir]; %# Find the index for directories
fileList = {dirData(~dirIndex).name}'; %'# Get a list of the files
if ~isempty(fileList)
fileList = cellfun(@(x) fullfile(dirName,x),... %# Prepend path to files
fileList,'UniformOutput',false);
end
subDirs = {dirData(dirIndex).name}; %# Get a list of the subdirectories
validIndex = ~ismember(subDirs,{'.','..'}); %# Find index of subdirectories
%# that are not '.' or '..'
for iDir = find(validIndex) %# Loop over valid subdirectories
nextDir = fullfile(dirName,subDirs{iDir}); %# Get the subdirectory path
fileList = [fileList; getAllFiles(nextDir)]; %# Recursively call getAllFiles
end
end
谢谢你的快速反应!但是我不确定要在代码中更改什么以便对我有用:(@User3582302我添加了一些注释以澄清每一行的作用。这有帮助吗?代码在哪些方面不起作用?它是否抛出错误或返回错误结果?我得到了以下错误:>>bla=datalaaded('D:\Desktop\BOP Metingen\')files={}来自非单元格数组对象的单元格内容引用。数据加载错误(第8行)如果isdir(tmpnames{i})%如果它是一个子目录而不是fix-mine,您可以使用以下答案:我试过了,但我只会给我一个大单元格..我无法从中加载/使用我的数据(从单元格格式的文本文件)