Code covered by the BSD License  

Highlights from
Serialize/Deserialize

4.25

4.2 | 4 ratings Rate this file 10 Downloads (last 30 days) File Size: 3.31 KB File ID: #29457

Serialize/Deserialize

by Tim

 

19 Nov 2010 (Updated 16 Dec 2010)

Encode and decode almost any matlab variable into a sequence of bytes.

| Watch this File

File Information
Description

This pair of functions can serialize and deserialize (i.e. encode and decode) almost any matlab object into a sequence of bytes. This will be useful for saving objects to disk, sending them over the network, or perhaps to other programs.

Any combination of structs, cells and matrices are supported. All the data types (i.e. all integer sizes, chars, bools, singles and doubles) are supported. They are automatically converted back to double on decoding, but you can easily change this.

I haven't tested it extensively, but it's quite short so any bugs should be easy to fix.

Usage is like this:

astruct.field1 = [1 2 3]; astruct.field2 = {'foo', 'bar', 'baz'};
serialized_astruct = serialize(astruct);
deserialized_astruct = deserialize(serialized_astruct);

After execution, astruct and deserialized_astruct should be equal.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
serialize
This submission has inspired the following:
DataHash, Fast serialize/deserialize

MATLAB release MATLAB 7.11 (2010b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (6)
01 May 2011 Jan Simon

Instead of a growing array, it would be faster to create a growing cell and cat the subvectors at the end.

11 May 2011 Mohammad Reza

needs a bit of work to make it work with logical values. typecast() doesnt cope with logicals.

08 Jun 2011 Jeff

This is a handy function. I noted a problem if you gave it an empty structure. But aside from that it seems to work for the datatypes I used it with.

27 Oct 2011 Hakon

Tim - thanks a lot - how could this be extended to also serialize functions / function handles?

31 Oct 2011 Tim

Hakon: You mean so you can send entire functions? That would be difficult, but check out func2str() and functions().

04 Jan 2012 Kay H. Brodersen

Tim, this is a great tool. In order to support boolean variables, you could add the following two lines before line 59 in serialize.m (version 16/12/210):

elseif islogical(v)
    m = [m; double(v)];

Please login to add a comment or rating.
Updates
16 Dec 2010

Fix bug with single numbers.

Tag Activity for this File
Tag Applied By Date/Time
serialize Tim 19 Nov 2010 12:45:15
encode Tim 19 Nov 2010 12:45:15
data Tim 19 Nov 2010 12:45:16
save Tim 19 Nov 2010 12:45:16
export Tim 19 Nov 2010 12:45:16
network Tim 19 Nov 2010 12:45:16
matfile Tim 19 Nov 2010 12:45:16
data Vital 04 Mar 2011 03:49:09

Contact us at files@mathworks.com