Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
951 views
in Technique[技术] by (71.8m points)

ascii - How can I load 100 files with similar names and/or string in just one step in MATLAB?

I have 100 ASCII files in my directory all named as follows:

int_001.ASC
int_002.ASC
int_003.ASC
.
.
.
int_099.ASC
int_100.ASC

I have to import them in MATLAB all with importdata, which should work as follows:

A = importdata('int_001.ASC', ' ', 9)
x = A.data(:,1)
y = A.data(:,2)

My question is: how can I avoid writing 100 times importdata? Is there a way to write the first string only and then have all data uploaded?

Thanks

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
fls = dir( 'int_*.ASC' );
for fi=1:numel(fls)
    A{fi} = importdata( fls(fi).name, ' ', 9 );
    % ...
end

UPDATE:
You may use string formatting to read the files according to their numbers:

for fi=1:100
    A{fi} = importdata( sprintf('int_%03d.ASC', fi ), ' ', 9 );
    % ...
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...