Borrar filtros
Borrar filtros

Fread problem...

19 visualizaciones (últimos 30 días)
jason beckell
jason beckell el 26 de En. de 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.

Respuestas (2)

Thomas
Thomas el 26 de En. de 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 el 26 de En. de 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);

Categorías

Más información sobre Large Files and Big Data en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by