2026-10-20 · 8 min read
- matlab
- json
- tooling
The MATLAB array converter is a pair of printers. JSON to MATLAB accepts a numeric array and writes brackets, spaces, and semicolons. MATLAB to JSON splits on semicolons and then on commas or whitespace, parses each token with Number(), and pretty-prints the arrays. Neither direction calls MATLAB.
The useful checks are the root type, the row count after empty pieces are dropped, and the token Number() actually parsed.
Map JSON shapes to one matrix line
A matrix [[1, 2, 3], [4, 5, 6]] prints as [1 2 3; 4 5 6] plus a newline. A flat array [1, 2, 3] prints as [1 2 3] plus a newline. A column written as [[1], [2]] prints as [1; 2]. An uneven pair [[1, 2], [3]] prints as [1 2; 3]. This converter does not reject a short row.
[] prints as []. A root that is not an array prints as [] too, with a newline and with Copy enabled. That includes the number 1, an object, null, and a JSON string. A nested array such as [[[1]]] and a matrix that contains a string both throw Matrix must contain only numbers. The wording covers the string case and the extra nesting with one sentence.
- Start from a JSON array when you want matrix text.
- Treat a flat array as a single MATLAB row.
- Look again at [] when the input was a number or an object.
- Keep uneven rows out of MATLAB even though this page accepts them.
Split rows before you parse tokens
Brackets are optional. Semicolons separate rows, and a row that trims to nothing is dropped. 1 2; is one row, so the JSON is the flat array 1, 2. [1 2; 3 4;] is two rows. 1,2;3,4 is two rows of two. Commas and spaces are the same separator, and 1,,2 becomes 1, 2.
One row is a JSON array. Two or more rows are an array of arrays. Pretty-print uses two spaces and a trailing newline. A scalar token such as -0 still comes back as a one-element array, not as a bare number.
Trust Number() on every token
0x10 becomes 16. 0b10 becomes 2. 01 becomes 1. 1.0 becomes 1. 1e2 becomes 100. 1e21 becomes 1e+21. -0 becomes 0. Infinity throws Invalid number: Infinity. Those are JavaScript number rules, not MATLAB literal rules. A hex token you meant to keep as text will not survive.
The same number rules show up on the way to MATLAB, because the JSON parser has already stored a number. 1e2 in a JSON array prints as 100 in the matrix line. 9007199254740993 prints as 9007199254740992.
Hand a rectangular decimal matrix to the next step
A whitespace-only box on the page returns an empty string and does not show []. Copy is disabled only when the output starts with Error:. An empty matrix that came from a non-array can still be copied, so read the input before you paste the brackets into a script.
Use the MATLAB array tool for the literal, the JSON formatter for the nested arrays, diff for a flat row against a two-row matrix, and the number-bases tool after you have replaced 0x10 with the decimal 16. The note should include the source token, the printed number, and whether the root was an array.