Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...