How can I write different length and different datatypes data to a text file from matlab
조회 수: 4 (최근 30일)
이전 댓글 표시
Right now I can only write a fixed length data to the text file ,but I need to change to a variable length data. My code:
fileID = fopen(logfilePathLocal,'at+');
formatSpec = '%s,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n';
fprintf(fileID,formatSpec,data{1,:});
fclose(fileID);
댓글 수: 0
채택된 답변
Jan
2017년 1월 25일
편집: Jan
2017년 1월 25일
Width = size(data, 2);
formatSpec = ['%s', repmat(',%d', 1, Width - 1), '\n'];
Or:
fileID = fopen(logfilePathLocal,'at+');
fprintf(fileID, '%s', data{1,1});
fprintf(fileID, ',%d', data{1,2:end});
fprintf(fileID, '\n');
fclose(fileID);
I don't know, what is faster or easier.
추가 답변 (1개)
Walter Roberson
2017년 1월 24일
You can compute the format specification. I often construct a format using repmat and [] concatenation.
댓글 수: 4
Jan
2017년 1월 25일
@Walter: Sorry, I have not seen your comment, because I had this thread opened for more than an hour and forgot to press the "relaod" button of my browser. The similarity with your answer was an accident.
참고 항목
카테고리
Help Center 및 File Exchange에서 Low-Level File I/O에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!