2026-10-18 · 6 min read · Rahul Chitturi
- matlab
- simulink
- json
Flashman's MATLAB struct tool parses one JSON object and prints an assignment per field. The sample object with Kp, Ki, maxTorque, and enabled becomes params.Kp = 1.2; params.Ki = 0.05; params.maxTorque = 120; and params.enabled = true. A root array or a root string is rejected with Root must be a JSON object. A trailing comma is a JSON syntax error before any MATLAB is printed.
A field name that matches an identifier, including a leading underscore such as _gain, is written with a dot. A name that does not, such as max-torque or 2fast, is written as a dynamic field: params.('max-torque') = 10; and params.('2fast') = 3. The parentheses are MATLAB syntax for a field name that cannot be a bare identifier.
Read null, quotes, and arrays as MATLAB values
null becomes []. A JSON string is wrapped in single quotes, and an apostrophe inside it is doubled, so it's fine becomes 'it''s fine'. An empty string becomes ''. true and false stay true and false. A numeric row such as [1, 2, 3] becomes [1 2 3], which MATLAB reads as one row, not as a column.
A matrix [[1, 2], [3, 4]] becomes [1 2; 3 4]. The semicolon is the row break. A JSON number written as 1e21 is still a number, and the assignment prints 1e+21. An integer wider than what a JavaScript number can store exactly is already rounded by the JSON parser before this conversion starts.
- Format the JSON before you convert it.
- Treat [] from null as an empty array, not as a missing field.
- Expect a numeric vector to be a row.
- Expect a hyphenated key to need parentheses.
Stop on nested objects and mixed arrays
A field whose value is an object throws Nested objects are not supported in struct fields yet. Flatten that object, or assign the inner fields yourself, before you paste the rest. An array that mixes numbers and strings throws Only numeric vectors/matrices are supported in arrays. There is no partial assignment file when the conversion throws.
A variable name that is not an identifier is replaced with params. The name 1bad still produces params.Kp. Check the left-hand side after you type a name, instead of assuming the box echoed what you typed.
Diff the assignments against the mask
Copy the generated lines into the review next to the JSON you formatted. Diff them against the previous mask script. A row vector that the model indexes as a column will run and still be the wrong shape. Use the MATLAB array tool when you need to inspect that matrix on its own, and the JSON formatter for the source object.
The struct tool does not open MATLAB and does not know the mask's units. The ticket should show the JSON field, the assignment, and the shape you expect Simulink to receive.