フィルターのクリア

Fread problem...

19 ビュー (過去 30 日間)
jason beckell
jason beckell 2012 年 1 月 26 日
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.

回答 (2 件)

Thomas
Thomas 2012 年 1 月 26 日
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 2012 年 1 月 26 日
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);

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by