How to put a string in a text file to define headers to data?

조회 수: 2 (최근 30일)
Alexandria Will-Cole
Alexandria Will-Cole 2018년 11월 15일
답변: Jeremy Hughes 2018년 11월 15일
Here is my code. I'm trying to make a header for columns of data, but everytime I run only the data remains in the file. Please help! Thanks.
for k=1:3
filename1=sprintf('s%d.txt',k);
fprintf(filename1, '# GHz S DB 50');
for j=1:3
M(j,1) = k;
M(j,2) = k;
M(j,3) = k;
dlmwrite(filename1,M,'-append','delimiter','\t');
end
end
  댓글 수: 2
Bob Thompson
Bob Thompson 2018년 11월 15일
Are you sure the headers are printing in the first place? Typically when I print headers for data I end up needing to open the file with fopen before the headers will print for me.
Alexandria Will-Cole
Alexandria Will-Cole 2018년 11월 15일
It's not. I am asking how to do so. Thanks!

댓글을 달려면 로그인하십시오.

채택된 답변

Jeremy Hughes
Jeremy Hughes 2018년 11월 15일
The issue is here:
fprintf(filename1, '# GHz S DB 50');
fprintf doesn't accept a file name, it need an open, writable file-id
fid = fopen(filename1,'w');
fprintf(fid, '# GHz S DB 50');
fclose(fid);
Then things should work as you expect

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by