Clear Filters
Clear Filters

How can I read in a key:value dictionary formatted string into a cell or structured array format?

29 views (last 30 days)
I would like to read the following pythonic dict string (returned by a system command) into either a struct or cell array. The containers.Map class requires pre-parsed cells of keys and values, but is there a pre-existing code for reading this?
{"xlo": -1.909999966621399, "yhi": 1.0, "xhi": 1.0, "n_triangles": 20676, "n_points": 10300, "flat_area_z": [], "n_edges": 32824, "zhi": 4.0, "ylo": -1.6299999952316284, "zlo": -5.8145337670434996e-18, "n_unmatched_edges": -136}
Thanks in advance!

Accepted Answer

Guillaume
Guillaume on 28 May 2017
Assuming you have R2016b or later, this is easily done with jsondecode:
str = '{"xlo": -1.909999966621399, "yhi": 1.0, "xhi": 1.0, "n_triangles": 20676, "n_points": 10300, "flat_area_z": [], "n_edges": 32824, "zhi": 4.0, "ylo": -1.6299999952316284, "zlo": -5.8145337670434996e-18, "n_unmatched_edges": -136}';
s = jsondecode(str)
m = containers.Map(fieldnames(s), struct2cell(s))
Note: in some earlier versions of matlab (at least R2016a) you can use the undocumented matlab.internal.webservices.fromJSON instead of jsondecode.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!