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

dpb
dpb 2016년 8월 1일
편집: dpb 2016년 8월 3일
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.

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

 채택된 답변

dpb
dpb 2016년 6월 24일

0 개 추천

fgetl is going to retrieve data from a file but *fopen(...,'w') discards any existing content in the file, if any, so you've always got an empty file to start with. Hence, there are no records so it does nothing except spin its wheels.
Not sure precisely what you want; are you trying to add some data to an existing file in some specific records, perchance? The only way to do that is to read the file with the data, echo it back to another file, write the new data when it's time, complete any remaining copy of existing data to the new file. Then close both files and if intending to modify the original, you can copy the new onto it and delete the temporary. Sequential files are, well... "sequential".
There is no Matlab function Write so it's not surprising it didn't do anything. There is a Java method write but I "know nuthink!" of it--whether it can write to a Matlab file handle I've no klew nor its expected syntax.
>> which Write
'Write' not found.
>> which write
write is a Java method % java.io.FileOutputStream method
>> write(i)
Undefined function 'write' for input arguments of type 'double'.
>> i
i =
3
>> write(num2str(i))
Undefined function 'write' for input arguments of type 'char'.
>>
well it didn't like either a double or a character string by its lonesome...
Add some more specifics of the expected result will allow more precise response(s).

댓글 수: 6

Write is my function name, I did not expect matlab to have a write file, I wrote this function under the name of write. But now let's say I create a dummy file with all lines containing a dummy character let's say space or 0 or 'hello'. I want write(iteration) to go to the line number num2str(iteration) and do
fprintf(fid,[nomrep,' \n']);
How to do that?
As dpb said,
"The only way to do that is to read the file with the data, echo it back to another file, write the new data when it's time, complete any remaining copy of existing data to the new file. Then close both files and if intending to modify the original, you can copy the new onto it and delete the temporary."
In the limited case where you are writing out something exactly the same size as what is already there, there is an alternative. You do not have that case, because you want to use 15 as your input and that takes exactly two characters to write out whereas your dummy would contain 1 character (space) or 1 character (0) or 5 characters ('hello').
Soheil  Esmaeilzadeh
Soheil Esmaeilzadeh 2016년 6월 24일
편집: dpb 2016년 6월 24일
It seems that if the file is empty matlab does not count the lines, so I thought of putting dummy alphabets let's say 'a' at each line, then print my desired parameter in the line that I like. So below I print 'a' in all 20 lines:
iteration=20;
fid=fopen('ID.dat','w');
for k=1:iteration
fprintf(fid,'\n %s','a');
end
status=fclose(fid);
fclose('all');
And now I call the function as Write(8), but again it does not print 8 at 8th line and prints it at the end of ID.dat file although I already resetted the line pointer by `frewind(fid);`
OK, my bad on missing Write as your function name...
Try
fid=fopen('ID.dat','a+');
in Write. Note the difference in doc fopen between the two 'append' options.
Also, if you want it to be an the N th line in the file, don't insert an extra newline before.
BUT, the real kicker is that when you write to the file, even if you get this far, the length data you added is going to overwrite the subsequent data and insert it and a newline obliterating the existing.
You don't seem to be grasping that a file is just a series of characters; records are only \n characters also just in series one after another in the file; it's nothing at all like the representation on a screen or in a text editor; it's just a string of bytes.
Walter Roberson
Walter Roberson 2016년 6월 24일
편집: Walter Roberson 2016년 6월 24일
If you use 'a+', which does allow reading and writing, then be sure to use frewind() before the first read, as the default positioning might be to end of file.
Files produced on MS Windows might use \r\n (carriage return then newline) to mark the end of records. If you are planning to modify a file in-place it is important that you do not open the file with 't' ("text") permissions, such as 'a+t', and that if you need \r in your output that you write them in explicitly.
dpb
dpb 2016년 6월 24일
Now Walter, don't confuse OP even more... :)
But, if it's an all-Matlab solution, 't' paired with '\n' does "the right stuff" automagically albeit at a performance overhead cost. One generally can ignore it; fortunately the apps that still are around for which it matters are getting to be more and more long-in-the-tooth so don't see the issues much any more.
Excepting, of course, one can create them for oneself in cases like this where trying to do "what does not come natcherly"; namely try to modify in-place a sequential file that is simply rife with issues that when one has to ask, one is unprepared to deal with...

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

질문:

2016년 6월 24일

편집:

dpb
2016년 8월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by