File Exchange

image thumbnail


version 1.0 (210 KB) by

A tool for mapping binary data to MATLAB structures and vice versa.



No License

cstruct maps bytes (i.e. binary data) to MATLAB structures and vice versa. It is very useful for unpacking binary data from a disk file, or preparing MATLAB data for sending over an interface.

The name 'cstruct' is derived from its original use of reading binary data from a socket interface whose format was based on a C structure. cstruct recasts the received bytes as a MATLAB data type (include simple types, and agregate types - structures and cells).

The mapping is based on a template. A MATLAB object is created that has the desired format/structure. cstruct analyzes the structure and can either extract the bytes from it, or pack bytes into the structure.

Various byte alignment and byte ordering options are supported.

Since I've written this, I've found it to be more useful that I thought, as I regularly deal with recorded data and binary data streams. It really helps bridge the gap between data created with C programs and MATLAB, without having to create specialty mex files, and without using the cumbersome fread function with data type specifiers.

I hope you'll find this useful.

I've provided the .DLL mex function for Microsoft Windows platforms, and the source code for compiling on other (including UNIX) platforms:
mex cstruct.c MatlabCStruct.c

Comments and Ratings (14)


Scott (view profile)

Please note that functions "mxCreateScalarDouble" are depreciated and no longer supported. You need to edit the functions and use "mxCreateDoubleScalar". Just replace those calls in the C code and mex will be able to compile

Ryan Groulx

I am trying to run this on MatLab 2014b 64bit. Not sure if i am doing something wrong, but one error i get is:

Attempt to execute SCRIPT cstruct as a function:

Error in binread (line 30)
attrib = cstruct(fmt,[],varargin{:});

I tried to make a MEX function, but also got an error:

>> mex cstruct.c MatlabCStruct.c
Building with 'Microsoft Visual C++ 2012 (C)'.
Error using mex
Creating library cstruct.lib and object cstruct.exp
cstruct.obj : error LNK2019: unresolved external symbol mxCreateScalarDouble referenced in function mexFunction
cstruct.mexw64 : fatal error LNK1120: 1 unresolved externals


Lior (view profile)

I am having problems with mixing types in GLNX86 Linux.

A simple matlab structure

simple.double1 = double(10.1);
simple.uint8 = uint8(66);
simple.double2 = double(10.1);

with structure definition in C as

typedef struct {
double double_1;
unsigned char uint8_1;
double double_2;
} simple;

is read from the binary file created by cstruct as

simple_str.double_1: 1.010000e+01
simple_str.uint8_1: 66
simple_str.double_2: 4.667261e-62

Hayden Callow

For users on linux, x86_64 the file MatlabStruct.c may need to be changed to match on disk data.

Lines 179,182 use sizeof long (which is 8 on linux64) for int32, 4 byte elements.

The types INT32_T and UINT32_T work well as a replacement

Hayden Callow

Jack Chue

Has anyone experienced issues with Matlab being column major order vs C being row major order for arrays? It doesn't look like cstruct handles this difference.


Mark (view profile)

Any change that this could be modified to handle complex data types?


Mark (view profile)

Y. M. Sun

Ruth L

Makes life easier. Thank you.

Carl Klopstein

Great tool for reading / writing binary files that can be swapped between platforms. Thanks!

Brian Thrall

Requires <windows.h> so dont bother if you are compiling for UNIX.

topo Z

Very good and usesull
finally I can read my binary files in matlab


Another fix to binread.m.

Thanks to Olaf Bousche for finding a minor bug. Fixed in this update.

MATLAB Release
MATLAB 6.5 (R13)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video