Problem reading a binary file in MATLAB
20 ビュー (過去 30 日間)
古いコメントを表示
I have a binary file that I have converted from .csv to .bin. The .csv file is attached.
I am using the following code to read the .bin file in MATLAB:
fn = 'sample.bin';
fid = fopen(fn, 'r');
dat = fread(fid, '*int16');
fclose(fid);
I have tried both int16 and int32 in the fread function. Still, MATLAB does not read the file correctly.
Original .csv: -1966965
Converted .bin: 00101101 00110001 00111001 00110110 00110110 00111001 00110110 00110101
MATLAB reads the .bin file as:
808529968
825241905
825241632
808464433
808460337
808530225
807416112
808530224
540029233
825307184
808530224
825241632
808464689
808460337
825241905
807415857
808530224
You can check the correct conversion on this website:
What should I change in my code so that MATLAB reads the .bin file correctly?
1 件のコメント
rsneha rani
2019 年 6 月 15 日
編集済み: rsneha rani
2019 年 11 月 22 日
Also check this site: binary to ascii
回答 (2 件)
Walter Roberson
2019 年 5 月 17 日
A = [808529968
825241905
825241632
808464433
808460337
808530225
807416112
808530224
540029233
825307184
808530224
825241632
808464689
808460337
825241905
807415857
808530224]
char(typecast(uint32(A),'uint8')).'
ans =
'00101101 00110001 00111001 00110110 00110110 00111001 00110110 00110'
0 件のコメント
Sulaymon Eshkabilov
2019 年 5 月 16 日
Hi,
Here is how you should write your data into a binary file and read it from the binary file.
% Writing in abinary file
A = -1966965;
FID1 = fopen('AA.bin', 'w+');
fwrite(FID1, A, 'float64'); % Precision is float64
fclose(FID1);
%% Reading from binary file:
clearvars
FID2=fopen('AA.bin', 'r');
[AAnew, count]=fread(FID2, [1, 8], 'float64'); % Precision is float64
Good luck.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Large Files and Big Data についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!