matlab读取文件夹中所有文件

matlab读取文件夹中所有文件,第1张

写一个for循环

每次修改文件

for i=1:803

...

if i<=9

name=['JULY7000',num2str(i),'.trtx']

elseif i<99

name=['JULY700',num2str(i),'.trtx']

else

name=['JULY70',num2str(i),'.trtx']

end

...

end

%%%%%%%%%%%%%%%%%%

问题补充知圆:目前关键问题是怎么把filenames中的文件名'JULY70001.trtx'带入到[x,y]=textread('e:\datas\JULY70001.trtx','%f %f','headerlines',2)命令中读数据

%%%%%%%%%%%%%%%%%%%

这不简单吗

filenames=...

patchname='e:\datas\'

fullname=[patchname,filenames]

[x,y]=textread(fullname,'%f %f','headerlines',2)

我不知道你这个函数用对了没有,

但是前面那几句话就可以实现文件名拼拦洞接搭衡塌

方法/步骤

获取第一层文件夹下的所有文件夹以及数据信息结构体。

例如:主路径是SourcePath

可以通过相对路径代码cd(SourcePath)File1NameFormation=dir('*.')获取当前路径下所有文件夹信息并基胡且将文件夹信息保存在File1NameFormation中。也可以使用绝对路径:File1NameFormation=dir('SourcePath\*.')对于需要列出什么类型的文件可以通过将'*.'改成'*.xxx';其中XXX为类型文件扩展名

对路径扩展进行计数,循环进行分文件夹读取

File1Number=numel(File1NameFormation)或者File1Number=size(File1NameFormation,1)通过获取当前文件夹中有多少层文件夹。

对得到厅耐的文件夹通过计数进行循环,进入次文件夹一次继续读取文件。

但是这里需要注意的是,如果是文件夹需要从第三个文件夹读取,因为'.'和'..'一个为当前文件夹一个为上一层文件夹,所以需要从第三个文件读取,可以通过第二个返回上一层文件夹。

可以发现进入子文件后就进入循环了,返回了第一步。在这里可以整合所有的代码

cd(SourcePath)

File1NameFormation=dir('*.')

File1Number=numel(File1NameFormation)

for LoopFile1Number=3:File1Number

NowPath=fullfile(SourcePath,File1NameFormation(LoopFile1Number).name

......................

end

回顾整个过程,重复写的代码很多,在这里我们可以通过运用递归的思维写一个读取数据代码。

这里通过循环来写读取文件函数。扮锋春

function ReadFile(MainPath,FileType,FileLayer)

%%%%%%%%%%%%%%

%MainPath为主路径,FileType为需要读取的文件类型,FileLayer为文件类型在哪一层文件下

cd(MainPath)

if(FileLay>=0)

PathFileFormation=dir('*.')

PathNumber=numel(PathFileFormation)

for LoopPathNumer=3:PathNumber

Path=fullfile(MainPath,PathFileFormation(LoopPathNumber).name)

ReadFile(Path,FileType,FileLayer-1)

end

else

PathFileFormation=dir('FileType')

PathNumber=numel(PathFileFormation)

for LoopPathNumer=3:PathNumber

Path=fullfile(MainPath,PathFileFormation(LoopPathNumber).name)

end

end


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12160843.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存