When I try to convert a single structure to array using struct2table() I get an error.
But if I have an array of that structure it works just fine.
Try the following code:
load singleStruct.mat
% This ends up in error
try
myTBL = struct2table(singleStruct);
catch
disp('I told you this ends up in error.');
end
% This wont end up in Error
structArray(1) = singleStruct;
structArray(2) = singleStruct;
myTBL = struct2table(structArray);
I have attached the singleStruct.mat for you guys to try.
Any work around?
Well, I usually have an array of those structure, this is really an special case that it is only one.

댓글 수: 1

Steven Lord
Steven Lord 2016년 4월 6일
What is the FULL text of the error message?

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

 채택된 답변

Matthew Eicholtz
Matthew Eicholtz 2016년 4월 6일
편집: Matthew Eicholtz 2016년 4월 6일

1 개 추천

You may have already noticed this, but the error is due to having fields with an unequal number of rows. When you pass a scalar structure as input to struct2table(), it expects every row to be a different row in the table. This is problematic if the number of rows for each field varies.
In your provided data, it looks like there are three fields causing this problem: ELEVB, ELEVB_FR, and SNOEB.
One simple workaround is to encapsulate those fields into 1x1 cell arrays.

댓글 수: 4

Mohammad Abouali
Mohammad Abouali 2016년 4월 8일
Once I have multiple of these structures, those fields that are an array, are automatically encapsulated as Cell arrays. However, it appears that once I have a single Structure, that is not done automatically and I have to do that manually on my own.
(I still think that this should be considered as a bug and fixed.)
Peter Perkins
Peter Perkins 2017년 4월 26일
This is not a bug. From the doc for struct2table:
'AsArray' — Indicator for how to treat scalar structure
Indicator for how to treat scalar structure, specified as the comma-separated pair consisting of 'AsArray' and either false, true, 0, or 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
This behavior is to make life easier in interactive use. Programmatically, if you are working with structure arrays that might be 1x1 arrays (as opposed to thinking of them as "scalar structs"), then you want to use AsArray, in the same way that you want to use DIM when calling mean on a matrix that might just have one row.
Austin Healy
Austin Healy 2018년 9월 20일
Thanks for the explanation and now that I see AsArray I'll remember to set it. I understand your point but frankly, I think the default value for AsArray should be true. In a programmatic setting I can't think of a situation where I would so radically want to change behavior when I happen to have a scalar array
Peter Perkins
Peter Perkins 2018년 10월 1일
That's why the parameter exists. Interactively, it would be a pain to type all that. Programmatically, it's a good idea for the reason you cite. It's similar to "always use the DIM argument to the mean function when you are writing a script or function that will be reused."

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

추가 답변 (1개)

Lukas
Lukas 2017년 4월 26일

3 개 추천

As this error only occurs with (1x1) structs, there is a simple workaroud. To convert a struct into a table, use the following code:
largeStruct = repmat(struct,2,1);
table = struct2table(largeStruct);
table = table(1,:);

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

태그

Community Treasure Hunt

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

Start Hunting!

Translated by