user3389597
user3389597

Reputation: 471

Matlab Input/output of Several Files

I do matlab operation with two data file whose entries are complex numbers. For example,

fName = '1corr.txt';
f = dlmread('1EA.txt',',');
fid = fopen(fName);
tline = '';

Then I do matrix and other operations between these two files and write my output which I call 'modTrace' as:

modTrace
fileID = fopen('1out.txt','w');
v = [(0:(numel(modTrace)-1)).' real(modTrace(:)) ].'; 
fprintf(fileID,'%d %e\n',v);

The question is, if I have for example 100 pairs of such data files, like (2corr.txt, 2EA.txt), ....(50corr.txt, 50EA.txt) how can I generalize the input files and how to write all the output files at a time?

Upvotes: 0

Views: 64

Answers (1)

Daniel
Daniel

Reputation: 36710

First of all, use sprintf to get your variable names depending on the current index.

corrName=sprintf('%dcorr.txt',idx);
EAName=sprintf('%dEA.txt',idx);
outName=sprintf('%dout.txt',idx);

This way, you have one variable (idx) which has to be changed.

Finally put everything into a loop:

n=100
for idx=1:n
    corrName=sprintf('%dcorr.txt',idx);
    EAName=sprintf('%dEA.txt',idx);
    outName=sprintf('%dout.txt',idx);
    f = dlmread(EAName,',');
    fid = fopen(corrName);
    tline = '';

    modTrace
    fileID = fopen(outName,'w');
    v = [(0:(numel(modTrace)-1)).' real(modTrace(:)) ].'; 
    fprintf(fileID,'%d %e\n',v);

end

Instead of hardcoding the number 100, you could also use n=numel(dir('*EA.txt')). It count's the files ending with EA.txt

Upvotes: 1

Related Questions