Jessy
Jessy

Reputation: 111

MATLAB how to write header in text file

How to write a text header in text file? for example in the example below, how to write the header code salay month just once?

Code Salary Month
12   1000   12
14   1020   11
11   1212   9 

The code:

fid = fopen('analysis1.txt','wt');
for i=1:10
   array = []; % empty the array
   ....
   array = [code salary month];
   format short g;
   fprintf(fid,'%g\t %g\t %g\n',array); % write to file
end
fclose(fid);

Upvotes: 4

Views: 19659

Answers (3)

Jaap
Jaap

Reputation: 21

Just to make it easy to copy and paste

fid = fopen('Output.txt','wt');
fprintf(fid, '%s\t %s\t %s\n', 'x','y1','y2');
% have a matrix M(N,3) ready to go
dlmwrite('Output.txt', M,'delimiter', '\t', '-append')
fclose(fid);

Jaap

Upvotes: 1

Sami
Sami

Reputation: 1

Thanks, Here is a script I modified to generate one variable file,

fid = fopen('vout.h','wt');
format short g;

fprintf(fid,' /* Header File for the variable vout */  \n\n' );

fprintf(fid,'int vout[ %g ] = { ' ,length(vout));

for i=1:length(vout)

   fprintf(fid,'%g,',vout(i)); % write to file
end
fprintf(fid,'} ; ');

fclose(fid);

Upvotes: -2

Josef Adamcik
Josef Adamcik

Reputation: 5790

Is there any reason for not using simple solution like following?

...
fid = fopen('analysis1.txt','wt');
fprintf(fid, '%s\t %s\t %s\n', 'Code','Salary','Month');
for i=1:10
   array = []; % empty the array
...

Upvotes: 6

Related Questions