flashman
← All posts

MATLAB matrix literal parsing debugging

Debug MATLAB matrix text the converter rewrites: a non-array becomes [], hex and binary prefixes become numbers, and a trailing semicolon drops a row.

2026-10-20 · 6 min read · Rahul Chitturi

  • matlab
  • json
  • debugging

The MATLAB array page converts a JSON array of numbers into matrix text, and matrix text back into pretty JSON. Brackets are optional on the way in. A comma and whitespace both split columns. The converter does not ask MATLAB to evaluate the text, and it does not check that every row has the same length.

Two results look like data loss. A JSON value that is not an array becomes an empty matrix with no error. A token Number() accepts, including 0x10 and 0b10, is printed as that function's decimal.

A non-array becomes an empty matrix

[[1, 2, 3], [4, 5, 6]] becomes [1 2 3; 4 5 6] and a newline. [1, 2, 3] becomes [1 2 3] and a newline, which is a row, not a column of three. [[1], [2]] becomes [1; 2]. [[1, 2], [3]] becomes [1 2; 3]. The uneven row is kept. [] becomes [].

The JSON number 1, the object {"a":1}, null, and a string all take the same path: the root is not an array, so the result is [] and a newline. Copy stays enabled, because the text does not start with Error:. A 3-deep array such as [[[1]]] throws Matrix must contain only numbers. A string inside a matrix throws that same message. The message names numbers even when the failure is nesting.

  • Confirm the root is an array before you trust [].
  • Accept a flat JSON array as one MATLAB row.
  • Accept uneven rows in this converter.
  • Read Matrix must contain only numbers as a shape failure too.

Number() reads the MATLAB tokens

On the way back to JSON, each token goes through Number(). 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. 1,,2 collapses to the array 1, 2 because empty pieces between commas are dropped.

One row stays a flat JSON array. 1, 2, 3 is [1, 2, 3]. A trailing semicolon does not create a second row: 1 2; is still [1, 2], and [1 2;] is the same. Two rows, as in 1,2;3,4, become an array of arrays. [1 2; 3 4;] drops the empty piece after the last semicolon and stays two rows. Pretty JSON uses two spaces and a final newline.

Keep the source digits next to the print

A whitespace-only box returns an empty string before either converter runs. The functions themselves treat an empty matrix literal as []. The page and the function disagree on that input, and the page is the one you are looking at.

Use the MATLAB array tool for the literal, the JSON formatter for the nested text, diff for a row against a two-row matrix, and the number-bases tool only after the token is already a decimal. Quote the source token and the printed number when 0x10 has become 16.

Try these tools