필터 지우기
필터 지우기

PROBLEM IN WRITING A text in MATLAB

조회 수: 2 (최근 30일)
Homayoon
Homayoon 2015년 9월 13일
편집: per isakson 2015년 9월 18일
Dear All,
Well I have a text that I have to fprintf it into a text file using my matlab code. But I cannot figure what the output is not what I expect. Is there anything wrong with my code?
Here is the text I need to be written in my output file:
dump_modify 1 format "%5d %5d %25.10g %25.10g %25.10g"
dump_modify 1 sort id
Here is my code:
fprintf(fid,'dump_modify 1 format "%5d %5d %25.10g %25.10g %25.10g" \n\')
fprintf(fid,'dump_modify 1 sort id\n\n')
but the output is something strange!
dump_modify 1 format "dump_modify 1 sort id
Thanks so much for helping me.

채택된 답변

Walter Roberson
Walter Roberson 2015년 9월 13일
fprintf(fid,'%s\n', 'dump_modify 1 format "%5d %5d %25.10g %25.10g %25.10g"');
fprintf(fid,'%s\n\n', 'dump_modify 1 sort id');
  댓글 수: 3
Homayoon
Homayoon 2015년 9월 13일
what was wrong with mine? can you say that please?
Walter Roberson
Walter Roberson 2015년 9월 13일
When you only supply fid and a string, the string is treated as a format. Your string looks like a format so MATLAB tried to use it like that. It got to the first %, tried to use it as a format specification, found there were no input arguments to go with it, and ended processing the string. Then it proceeded to the next fprintf() call, which did not happen to contain any % format characters so it got processed as-is.
When you code as fprintf(fid, '%s', SomeString) then the SomeThing gets copied in exactly without being processed for format characters, as that is what a %s format specification means, to copy the string exactly.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by