Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

how to merge text files

Asked by Lalit Patil on 8 Nov 2012
for k = 1:100
fname = ['Image' num2str(k) '.txt'];
fid = fopen(fname,'wt');
for j=1:length(R)
fprintf(fid,'%f %f %f\n',R(j),C(j) ,Z(j));
end
fclose(fid);
end

I got 100 text files from this code, In it R,C,Z will be saved as columns.. Now, i want to merge all this text files in single text file as R,C,Z columns.. so, how to do..?

0 Comments

Lalit Patil

Products

No products are associated with this question.

2 Answers

Answer by Walter Roberson on 8 Nov 2012
Accepted answer

OS-X and Linux version:

!cat Image???.txt > all_Image.txt

No point in writing code when the operating system already has the facilities.

3 Comments

Lalit Patil on 8 Nov 2012

Image1.txt will be the name of text file created from Image1.jpg ans so, Image2.txt, Image3.txt,...

Lalit Patil on 8 Nov 2012

I have done same thing for excel files,and it works but it takes too long time..

m   = 0;
for j = 1:100
global filename
filename = ['Image' num2str(j),'.xls'];
w = xlsread(filename,1,'A1:A1000');
u = xlsread(filename,1,'B1:B1000');
t = xlsread(filename,1,'C1:C1000');
q = length(w);
p = m+1;
R(1, p:(m+q)) = w;
R(2, p:(m+q)) = u;
R(3, p:(m+q)) = t;
xlswrite('lalits.xls',R');
y = xlsread('lalits',1,'A1:A65565');
m = length(y);
end
Walter Roberson on 8 Nov 2012

I misread your code and thought you were using 001 and so on. The correction would be

!cat Image*.txt > all_Image.txt

How you calculated the values in the vectors (e.g., by processing .JPG files) is not important to the task of merging the text files.

Each time you start up xlsread() or xlswrite() a new ActiveX connection is made to Excel (if you are using MS Windows.) That is slow. Your code for doing the appending is also pretty inefficient, writing to the xls file and reading it back immediately.

outfid = fopen('all_Image.txt', 'wt');
for j = 1 : 100
  filename = ['Image' num2str(j),'.txt'];
  fwrite(outfid, fileread(filename));
end
fclose(outfid)
Walter Roberson
Answer by Lalit Patil on 8 Nov 2012

Thanks for help... I got solution..

0 Comments

Lalit Patil

Contact us