No License

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

» Watch video

4.8 | 8 ratings Rate this file 7 Downloads (last 30 days) File Size: 210 KB File ID: #4048 Version: 1.0



AJ Johnson (view profile)


08 Oct 2003 (Updated )

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

| Watch this File

File Information

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

MATLAB release MATLAB 6.5 (R13)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (13)
17 Feb 2016 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

Comment only
14 Sep 2014 Lior

Lior (view profile)

21 Aug 2013 Jose Alejandro

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

Comment only
29 Jan 2010 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

Comment only
29 Jan 2010 Hayden Callow

14 Jan 2010 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.

04 Jun 2009 Mark

Mark (view profile)

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

Comment only
04 Jun 2009 Mark

Mark (view profile)

03 Apr 2006 Y. M. Sun

11 Nov 2005 Ruth L

Makes life easier. Thank you.

06 Oct 2005 Carl Klopstein

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

22 Jun 2004 Brian Thrall

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

Comment only
24 Feb 2004 topo Z

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

13 Nov 2003

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

19 Nov 2003

Another fix to binread.m.

Contact us