Code covered by the BSD License  

Highlights from
Serialize/Deserialize

4.33333
4.3 | 6 ratings Rate this file 16 Downloads (last 30 days) File Size: 3.31 KB File ID: #29457 Version: 1.1

Serialize/Deserialize

by

Tim (view profile)

 

19 Nov 2010 (Updated )

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

Serialize inspired this file.

This file inspired Fast Serialize/Deserialize and Data Hash.

MATLAB release MATLAB 7.11 (R2010b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (10)
12 Jun 2014 Sebastian

I found the following problems with your functions:
1. fieldless structures like "struct()" and logicals like "false" throw an error (already mentioned)
2. Empty structures (even with fields) result in an empty array
3. Data types other than double are always returned as double, even if the class seems to be serialized

Comment only
08 May 2014 Ralph Coleman

In deserialize_object, you should distinguish between graphical handles and handles to objects defined with classdef. I modified the code in the following way to get it to work:
if ishandle(v)
set(v,fn{1},conts.(fn{1}));
else
v.(fn{1}) = conts.(fn{1});
end

13 Feb 2013 Nath

Nath (view profile)

typecastx would give better perf than typecast.
It is also possible to avoid a few loops. I tested this and it runs faster (modify the serializeCell subfunction)
% Dimensions.
% for ii = 1:ndims(v)
% m = [m; typecastx(uint32(size(v, ii)), 'uint8').'];
% end
m = [m; typecastx(uint32(size(v)), 'uint8').'];

% Just serialize each member.
% for ii = 1:numel(v)
% m = [m; serialize(v{ii})];
% end
m0= cellfun(@serialize,v(:),'uni',0);
m= cat(1,m,m0{:});

Comment only
05 Dec 2012 Yu Ang Tan  
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)];

31 Oct 2011 Tim

Tim (view profile)

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

Comment only
27 Oct 2011 Hakon

Hakon (view profile)

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

08 Jun 2011 Jeff

Jeff (view profile)

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.

11 May 2011 Mohammad Reza

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

01 May 2011 Jan Simon

Jan Simon (view profile)

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

Comment only
Updates
16 Dec 2010 1.1

Fix bug with single numbers.

Contact us