fwrite

5 Ansichten (letzte 30 Tage)
Tor Fredrik Hove
Tor Fredrik Hove am 18 Nov. 2011
I tried to use fwrite which is described as saving in binary form but it saved it in numbers. At least my attempt did:
Created a new file rain2.tv and atached too numbers to it and read it afterwards:
>> fid=fopen('rain2.tv','w')
fid =
3
>> fwrite(fid, '2 4')
ans =
3
When i open the file rain2.tv it is in normal numbers not binary and when I look at the saved file it is also saved in normal

Akzeptierte Antwort

Titus Edelhofer
Titus Edelhofer am 18 Nov. 2011
Hi Tor,
1) you save strings 2,4, not values (pass [2 4] instead of '2 4' to fwrite
2) you should pass the format ('double'), see fwrite doc
Titus
  2 Kommentare
Tor Fredrik Hove
Tor Fredrik Hove am 18 Nov. 2011
I have read doc fwrite but I can't seem t oget a specific example. I have written:
>> fid=fopen('rain2.tv','w')
fid =
3
>> fwrite(fid, [1 2])
ans =
2
>> fclose(fid)
ans =
0
>> fopen('rain2.tv')
ans =
3
>> while ~feof(fid)
fgetl(fid)
end
ans =
>>
the ans is empty and noting is saved in the file
Walter Roberson
Walter Roberson am 18 Nov. 2011
The ans is not empty: I can see from what you posted that it contains two characters, binary 1 and binary 2.
fgetl() is used for reading *text*, not for reading binary. You would use fread() to read binary.
fread(fid)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by