开发者

MATLAB how to write header in text file

开发者 https://www.devze.com 2023-01-03 11:08 出处:网络
How to write a text header in text file? for example in the example below, how to write the he开发者_C百科ader code salay month just once?

How to write a text header in text file? for example in the example below, how to write the he开发者_C百科ader 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);


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号