MATLAB: How to read in a key:value dictionary formatted string into a cell or structured array format

containers.mappython

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!

Best Answer

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.
Related Question