fwrite

5 visualizzazioni (ultimi 30 giorni)
Tor Fredrik Hove
Tor Fredrik Hove il 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

Risposta accettata

Titus Edelhofer
Titus Edelhofer il 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 Commenti
Tor Fredrik Hove
Tor Fredrik Hove il 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 il 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)

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by