File Exchange

image thumbnail

Serialize/Deseriali​ze

version 1.1 (3.31 KB) by

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

4.33333
6 Ratings

5 Downloads

Updated

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.

Comments and Ratings (10)

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

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

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

Yu Ang Tan

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

Tim

Tim (view profile)

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

Hakon

Hakon (view profile)

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

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.

Mohammad Reza

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

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.

Updates

1.1

Fix bug with single numbers.

MATLAB Release
MATLAB 7.11 (R2010b)
Acknowledgements

Inspired by: serialize

Inspired: Fast serialize/deserialize, DataHash

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video