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

채택된 답변

Jan
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
Walter Roberson 2017년 1월 24일
You can compute the format specification. I often construct a format using repmat and [] concatenation.
  댓글 수: 4
Jan
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 CenterFile 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!

Translated by