Matlab: NEMOH printing data on a specific line
이전 댓글 표시
I have below function:
function [ output_args ] = Write(iter)
status=close('all');
nomrep=num2str(iter);
fid=fopen('ID.dat','a');
frewind(fid);
for k=1:iter
fgetl(fid);
end
fprintf(fid,['\n','% g \n',nomrep,' \n'],length(nomrep));
status=fclose(fid);
end
I expect that `Write(15)` creates ID.dat and prints 2 and 15 in consecutive lines.
But is prints those values always on the beginning of the file.
Even I tried `fgetl(fid)` alone, and also replaced for loop with while loop still did not work.
Is it due to the fact that I should fill in the lines before that with some dummy space? along side this, I executed
for i=1:5
Write(i);
end
Which should print 1 to 5 in each line but even this does not work.
댓글 수: 1
Write opens a file with the a append flag, true, but you then frewind the file which goes to the beginning. You then try to space a number of records into the file, but unless there is already data in the file, that does nothing. What you'll get from the final fprintf is indeed the two numbers (preceded by an extra(?) newline).
If there are supposed to be records before this, then yes, at some point you'll have to write them into the file (and not overwrite or delete them later).
ADDENDUM That is, whatever content that is in the file has to be explicitly put there; simply trying to move to subsequent "records" by the expedient of reading is NOT sufficient to cause the i/o routines to create records where none existed previously.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!