De-serialze char array to object

Hi,
I'm trying to convert a char array to an object. The char array used to be a MATLAB object which has been serialized and has a structure like this:
hBeams.stackUseStartPower = 1
hBeams.stackUserOverrideLz = false
hChannels.channelAdcResolution = {12 12 12 12}
hChannels.channelDisplay = [1;2;3;4]
(This is just a small portion of the char, there is much more data in reality).
Basically, I wat to de-serialze the string and recover the object so I can access its values.
When I use eval() it parses the string into an object and stores it in the current workspace. However, I need to store the data in a variable of another object.
Also, since it is usually not recommended to use eval(), is there a better (more elegant) way to do this?
Best,
Gebhard

Antworten (1)

Stephen23
Stephen23 am 22 Nov. 2018

0 Stimmen

5 Kommentare

Gebhard Stopper
Gebhard Stopper am 22 Nov. 2018
I have no control about saving the data. This is done by a third-party application.
The only thing I get is a long char array with the text representation of an object.
The serialization /de-serialization functions from the file exchnage only work when used together. Feeding them my char array doesn't work.
Stephen23
Stephen23 am 22 Nov. 2018
Bearbeitet: Stephen23 am 22 Nov. 2018
"This is done by a third-party application."
"The only thing I get is a long char array with the text representation of an object."
Ouch, what a badly designed application!
Easy-but-ugly solution: Print the character data to a script, and then run that script. Or evaluate the string. Either way that badly designed code forces you to do something ugly and inefficient.
Complex-but-efficient solution: You might be able to write a parser for that character array, if you have a specification for the kind of data that it contains, how it is structured, etc..
Guillaume
Guillaume am 22 Nov. 2018
Ouch, indeed!
Third option: ditch the 3rd party application. It doesn't inspire much confidence. What other gremlins does it hide?
Gebhard Stopper
Gebhard Stopper am 22 Nov. 2018
"Third option: ditch the 3rd party application. It doesn't inspire much confidence. What other gremlins does it hide?"
That is not an option. We need that software to run our Microscopes...
The char array is stored in TIFF files as metadata, holding all kinds of parameters, so I guess there are not many options how to store that data...
I think I'll stick with the eval() option for now and try to get a better solution when I have a more time to implement it.
so I guess there are not many options how to store that data
There are plenty of well established techniques for storing metadata. In TIFF files, they could have used private tags (and documented them). Alternatively, they could have used common serialisation formats instead of inventing their own. The two most common serialisation formats, json and xml, are both supported by matlab and can be transformed into a single variable in one line:
settings = jsondecode(serialisedstream);

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 22 Nov. 2018

Kommentiert:

am 22 Nov. 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by