MATLAB how to write header in text file

2019-01-26 16:27发布

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);

3条回答
三岁会撩人
2楼-- · 2019-01-26 17:02

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
...
查看更多
Melony?
3楼-- · 2019-01-26 17:03

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);
查看更多
闹够了就滚
4楼-- · 2019-01-26 17:09

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

查看更多
登录 后发表回答