MATLAB how to write header in text file

邮差的信 提交于 2019-12-29 07:56:09

问题


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

回答1:


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
...



回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/3052062/matlab-how-to-write-header-in-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!