No BSD License  

Highlights from
cstruct

4.71429

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

cstruct

by AJ Johnson

 

08 Oct 2003 (Updated 23 Jun 2004)

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  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (10)
24 Feb 2004 topo Z

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

22 Jun 2004 Brian Thrall

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

06 Oct 2005 Carl Klopstein

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

11 Nov 2005 Ruth L

Makes life easier. Thank you.

03 Apr 2006 Y. M. Sun  
04 Jun 2009 Mark  
04 Jun 2009 Mark

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

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.

29 Jan 2010 Hayden Callow  
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

Please login to add a comment or rating.
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.

23 Jun 2004

Provided portability to UNIX platforms.

Tag Activity for this File
Tag Applied By Date/Time
data import AJ Johnson 22 Oct 2008 07:08:35
data export AJ Johnson 22 Oct 2008 07:08:35
binary AJ Johnson 22 Oct 2008 07:08:35
data AJ Johnson 22 Oct 2008 07:08:35
c AJ Johnson 22 Oct 2008 07:08:35
language AJ Johnson 22 Oct 2008 07:08:35
structure AJ Johnson 22 Oct 2008 07:08:35
file AJ Johnson 22 Oct 2008 07:08:35
socket AJ Johnson 22 Oct 2008 07:08:35

Contact us at files@mathworks.com