No BSD License  

4.71429

4.7 | 7 ratings Rate this file 26 Downloads (last 30 days) File Size: 210 KB File ID: #4048

cstruct

by

 

08 Oct 2003 (Updated )

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

| Watch this File

File Information
Description

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 (11)
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

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

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

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

04 Jun 2009 Mark  
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.

24 Feb 2004 topo Z

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

Updates
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