Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to read from binary file in Octave

📅 2014-May-12 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ binary, file, octave, read ⬩ 📚 Archive

The fopen and fread functions can be used to read binary data from a file. Assume, we have stored the values of a matrix of 100 rows and 100 columns in binary format in a file. Such a file could be produced from a C or C++ program. Let us also assume that that data type of the values is short of 2 bytes.

To read the data from this binary file and reconstruct the original matrix:

> bfile = fopen('matdata.bin');
> bdata = fread(bfile, 'short');
> size(bdata)
ans =
  10000 1
> B = reshape(bdata, 100, 100);

These functions are from Matlab and so will work in it too.

Tried with: Octave 3.8.1 and Ubuntu 14.04


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧