Fread problem...

24 visualizzazioni (ultimi 30 giorni)
jason beckell
jason beckell il 26 Gen 2012
Hello to everyone!
I have a problem with the following simple portion of code:
b = rand(5, 1);
fid = fopen('prova.bin', 'w');
fwrite(fid, b);
fclose(fid);
% Read the contents back into an array
fid = fopen('prova.bin');
B = fread(fid, 5, 'double');
fclose(fid);
Fread doesn't seem to work, how come ? Has any of you got an idea?
Thank you very much and my best regards! Jason.

Risposte (2)

Thomas
Thomas il 26 Gen 2012
Add type 'double' in your fwrite
b = rand(5, 1);
fid = fopen('prova.bin', 'w');
fwrite(fid, b,'double'); % add type double here
fclose(fid);
% Read the contents back into an array
fid = fopen('prova.bin');
B = fread(fid, 5, 'double');
fclose(fid);
should work

Bård Skaflestad
Bård Skaflestad il 26 Gen 2012
You need to specify the precision of the data you output using fwrite is double. Otherwise, the subsequent fread operation fail. I'd write the above as
b = rand(5, 1);
fid = fopen('prova.bin', 'w');
fwrite(fid, b, 'double');
fclose(fid);
% Read the contents back into an array
fid = fopen('prova.bin');
B = fread(fid, 5, 'double');
fclose(fid);

Categorie

Scopri di più su Large Files and Big Data in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by