matlab filesep,在matlab里读取多个文件

在一个matlab的学习群里,有人提出了这么一个问题:在matlab里如何读取多信文件的数据,例如,有许多数据,存放在不同的txt文件里。

我的办法是把这些文件的文件名按一定的规律命名,假如有一百个数据文件,则可以把这些文件分别命名为:filename1.txt,filename2.txt,...,fielname100.txt,在读取的时候则可以使用循环:

for i = 1:100

fileName = ['filename' num2str(i) '.txt'];

x = load(filiName);

end

但另一位朋友给出了另一个解决的办法,无需对数据文件的文件名进行修改,就是文件名无须有规律,他的办法是:

A = dir(fullfile('d:\datafile','*.txt'));

这个语句是把存放数据文件的目录d:\datafile下的所有txt文件列出来,并把这些文件名的信息存放到一个变量A中,A是一个结构体变量,只要对A进行循环就可以读取到所有文件的数据了。

所谓人外有人,山外有山,我真是受教了,特写此文以记录此法。

附:help fullfile

help fullfile

FULLFILE Build full filename from parts.

FULLFILE(D1,D2, ... ,FILE) builds a full file name from the

directories D1,D2, etc and filename FILE specified.  This is

conceptually equivalent to

F = [D1 filesep D2 filesep ... filesep FILE]

except that care is taken to handle the cases where the directory

parts D1, D2, etc. may begin or end in a filesep. Specify FILE = ''

to build a pathname from parts.

Examples

To build platform dependent paths to files:

fullfile(matlabroot,'toolbox','matlab','general','Contents.m')

To build platform dependent paths to a directory:

addpath(fullfile(matlabroot,'toolbox','matlab',''))

See also filesep, pathsep, fileparts.

Reference page in Help browser

doc fullfile

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值