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

9 vues (au cours des 30 derniers jours)
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!

Réponse acceptée

Guillaume
Guillaume le 28 Mai 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.

Plus de réponses (0)

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by