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

조회 수: 23 (최근 30일)
Alex
Alex 2017년 2월 27일
댓글: Peter Perkins 2017년 2월 28일
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?

답변 (1개)

Walter Roberson
Walter Roberson 2017년 2월 28일
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
Walter Roberson
Walter Roberson 2017년 2월 28일
That was not your question. You wrote,
"This function should not have this error. It should work exactly the same and produce a table with m columns and 1 row. "
Which is contrary to the documented behavior of the function. You want to impose behavior upon the function designers instead of reading the documentation to find out what the function does.
'AsArray' — Indicator for how to treat scalar structure
false (default) | true | 0 | 1
true struct2table converts S to a table with one row and n variables. The variables can be different sizes.
false struct2table converts a scalar structure array with n fields into an m-by-n table. Each field must have m rows. This is the default behavior
Peter Perkins
Peter Perkins 2017년 2월 28일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by