Fread problem...

23 vues (au cours des 30 derniers jours)
jason beckell
jason beckell le 26 Jan 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.

Réponses (2)

Thomas
Thomas le 26 Jan 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 le 26 Jan 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);

Catégories

En savoir plus sur Large Files and Big Data dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by