Anyone help me with the format of fprintf

조회 수: 1 (최근 30일)
QIAO WANG
QIAO WANG 2019년 4월 30일
댓글: Star Strider 2019년 5월 1일
Hi,
I'm stuck with fprintf when I want to obtain this type of line in my text file.
With the code below, I'm able to get a line in my text file 31 -1 1 -1 30 -1 3 -1 7 -1 11 -1 16 -1 17 -1 21 -1 -2 -1 However, I don't want the last -1 in my output line. I want the output like this 31 -1 1 -1 30 -1 3 -1 7 -1 11 -1 16 -1 17 -1 21 -1 -2 What I have got is quite close to my final one but I really don't know how to modify my current code to achieve my final version.
str = [num2str(79), '.txt'];
fileID = fopen(str,'w');
temparray = [31 1 30 3 7 11 16 17 21];
formatSpec = '%d -1 ';
fprintf(fileID,formatSpec,temparray,-2);
Hope someone could help me out. Thank you in advance.

채택된 답변

Star Strider
Star Strider 2019년 4월 30일
The easiest solution is likely to modify your first fprintf statement, and add a second fprintf to print the final ‘-2’:
fprintf(fileID,formatSpec,temparray);
fprintf(fileID,' -2')
  댓글 수: 2
QIAO WANG
QIAO WANG 2019년 5월 1일
Brilliant! Many Thanks.
Star Strider
Star Strider 2019년 5월 1일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by