File Exchange

image thumbnail


version (3.31 KB) by Tim
Encode and decode almost any matlab variable into a sequence of bytes.


Updated 16 Dec 2010

View License

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.

Cite As

Tim (2020). Serialize/Deserialize (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (11)


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

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)
v.(fn{1}) = conts.(fn{1});


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{:});

Yu Ang Tan

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)];


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


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


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.

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


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


Fix bug with single numbers.

MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: serialize

Inspired: DataHash, Fast serialize/deserialize