flashman
← All posts

MATLAB column-major JSON array debugging

Debug MATLAB and JSON array mismatches by tracing column-major layout, row versus column vectors, nested lists, empty shapes, and numeric precision loss.

2026-09-29 · 6 min read · Rahul Chitturi

  • matlab
  • json
  • api

MATLAB stores array elements in column-major order, but JSON has no column-vector type. jsonencode follows the array's shape: a 1-by-n row becomes one JSON array, while an n-by-1 column becomes an array of one-element arrays.

Clients that expect [1,2,3] then fail validation, or they wrap every sample because a transpose happened earlier in a script and nobody checked size before encoding.

Record shape before you encode

Save size, class, and whether the value is a row, a column, or a full matrix. Also save the exact jsonencode text. A display of the numbers is not enough, because a column and a row can print as the same list in a log that drops shape.

  • Note rows, columns, and any trailing singleton dimensions.
  • Check isrow, iscolumn, and ismatrix.
  • Keep the JSON text next to the MATLAB expression.
  • Look for a struct field if the array is nested in jsonencode of a struct.

Match orientation to the contract

jsonencode([1 2 3]) produces [1,2,3]. jsonencode([1;2;3]) produces [[1],[2],[3]]. A matrix is encoded as a JSON array of rows, which matches MATLAB's display and not a flat column-major buffer.

(:) flattens in column-major order. Use transpose or reshape only when the API contract names the shape. Guessing a transpose to make a test pass will invert the next matrix.

Check empties, NaN, and the decode trip

Use Flashman's MATLAB array helper to inspect shape, the JSON formatter for the encoded text, diff to compare a row and a column encoding, and the MATLAB struct helper when the array is one field of a larger payload.

0-by-n and n-by-0 are different shapes. jsonencode turns NaN and Inf into null, so the JSON no longer tells them apart. jsondecode of a flat array returns a row, which means a column vector does not survive a round trip unchanged.

Try these tools