How to save variable as binary file?

I have a variable dimension double in workspace.This contains 4 individual physiological data in 4 rows. I want to save this variable with lowest possible size in the current folder. If I am not wrong the file format should be binary. How do I save this variable in current folder as a .bin file sothat later I can load it and work with it?

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 9 月 30 日

2 投票

Save in a .mat file(); you will get much better compression: https://www.mathworks.com/help/matlab/import_export/mat-file-versions.html. In the bin file, each double number will take a full 8 bytes.

5 件のコメント

Jhon Gray
Jhon Gray 2020 年 9 月 30 日
編集済み: Jhon Gray 2020 年 9 月 30 日
okay. Thanks a lot
Jhon Gray
Jhon Gray 2020 年 9 月 30 日
編集済み: Jhon Gray 2020 年 9 月 30 日
However, I want to check how much the bin file take. Can you please help me with the code. I have seen binary file write function which needs to load a binary file. But can't figue out how to create one.
Ameer Hamza
Ameer Hamza 2020 年 9 月 30 日
See the following example
rng(0);
M = rand(1000); % create matrix
save('data.mat', 'M'); % save using .mat
% save in .bin file
f = fopen('data.bin', 'w');
fwrite(f, M, 'double');
fclose(f);
% load from .bin file
f = fopen('data.bin', 'r');
data = reshape(fread(f, inf, 'double'), size(M)); % binary file also does not store the matrix dimensions by default
fclose(f);
Jhon Gray
Jhon Gray 2020 年 9 月 30 日
Thanks a lot
Ameer Hamza
Ameer Hamza 2020 年 10 月 1 日
I am glad to be of help!

サインインしてコメントする。

その他の回答 (0 件)

製品

リリース

R2020a

質問済み:

2020 年 9 月 30 日

コメント済み:

2020 年 10 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by