Why does struct2table not work if you don't have more than 1 entry per field?

47 views (last 30 days)
If I have a 1 x n struct with m fields, where n > 1, I have no issues with this function. If I have a 1 x 1 struct with m fields, I get this error:
Error using table.fromScalarStruct (line 480)
Fields in a scalar structure must have the same number of rows.
This function should not have this error. It should work exactly the same and produce a table with m columns and 1 row. Why is this not happening?

Answers (1)

Walter Roberson
Walter Roberson on 28 Feb 2017
If you look at https://www.mathworks.com/help/matlab/ref/struct2table.html#examples you can see that the expectation for a scalar structure is that multiple rows are created.
  3 Comments
Peter Perkins
Peter Perkins on 28 Feb 2017
The answer to "why is it designed that way" is that the majority of cases that involve converting a scalar struct to a table are cases where each field should be converted to a table variable. But you don't have a "scalar struct" in that sense, you have a "1x1 struct array". As Walter says, that's what the AsArray parameter is for.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!