How can I write both number and text to a file?
조회 수: 16 (최근 30일)
이전 댓글 표시
I want to write something like this:
BEGIN 0.00 0,0 0.5,1 1,1.5 1.5,1.75 14,2
from matlab to a text file. There are supposed to be a new number on one line, going down. I also have to go this several times for different values. How can I do this effectively?
댓글 수: 0
채택된 답변
Chaya N
2016년 10월 19일
편집: Chaya N
2016년 10월 19일
The easiest way to do this would be to write the entire line as a string into a text file, something like:
x = 'BEGIN 0.00 0,0 0.5,1 1,1.5 1.5,1.75 14,2';
fid = fopen('SomeTextFile.txt','a');
fprintf(fid,'%s\n',x);
fclose(fid);
You would have to reassign x every time with your new data. Does this help?
댓글 수: 3
Massimo Zanetti
2016년 10월 19일
편집: Massimo Zanetti
2016년 10월 19일
This thing doesn't print anything. Try adding file identifier in the fprintf command..
fprintf(fid,'%s\n',x);
추가 답변 (1개)
Massimo Zanetti
2016년 10월 19일
편집: Massimo Zanetti
2016년 10월 19일
Look here:
Here it is:
x = 'BEGIN 0.00 0,0 0.5,1 1,1.5 1.5,1.75 14,2';
fid = fopen('SomeTextFile.txt','a');
fprintf(fid,'%s\n',x);
fclose(fid);
But, for more complex inputs I suggest to read the help page. Will be useful.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import and Export에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!