How to make a table for a struct that contains elements with more than one number?

A struct is defined and it adds values as going: X.a, X.b, X.c. etc.most of them is a single value, but a few are arrays with multiple values, such as X.g(1), X.g(2), etc.
When convert them to table using:
thetable = array2table(X, 'RowNames', FileName, 'VariableNames', structNames);
fig = uifigure('Name', 'Variable');
uitable(fig, 'X', thetable);
It displays a very long horizontal table with correct display for those of single value elements. But for those array elements, it displays 2Xdouble, 3Xdouble, etc. not numbers.
The questions are:
(1) How to display those array element separately to view every value?
(2) How to rotate the table?
Thank you.

 採用された回答

Voss
Voss 2025 年 2 月 5 日
X = struct('a',1,'b',22,'c',1:3);
FileName = "SoMe/fIlE\NaMe";
thetable = struct2table(X, 'RowNames', FileName);
fig = uifigure('Name', 'Variable');
uitable(fig, 'Data', thetable);

7 件のコメント

John
John 2025 年 2 月 5 日
編集済み: John 2025 年 2 月 5 日
This sounds good.
How about when X is an array X{1}, X{2}, etc...?
Also, how to display the table in vertical format?
Thanks.
You're welcome!
"How about when X is an array X{1}, X{2}, etc...?"
I guess you mean when X is a cell array, given the X{1} syntax. In that case, what to do depends on what's in each cell of X (i.e., what is X{1}, X{2}, etc.?).
"how to display the table in vertical format?"
Maybe something like this:
X = struct('a',1,'b',22,'c',1:3);
FileName = "SoMe/fIlE\NaMe";
thetable = table( ....
struct2cell(structfun(@mat2str,X,'UniformOutput',false)), ...
'RowNames',fieldnames(X), ...
'VariableNames',FileName);
fig = uifigure('Name', 'Variable');
uitable(fig, 'Data', thetable);
John
John 2025 年 2 月 10 日
Thanks! This vertical display works for the single struct.
Now, the key point is that the cell is an array of struct. It can be saved and load and viewed.
However, to similate it and play with the table, this assignment doesn't work:
X{1} = struct('a',1,'b',22,'c',[5 7 9]);
X{2} = struct('a',4,'b',99,'c',[12 56 76]);
X{1} = struct('a',1,'b',22,'c',[5 7 9]);
X{2} = struct('a',4,'b',99,'c',[12 56 76]);
That assigment will work if X either doesn't already exist or is an existing cell array (not some other type of variable).
You can also create X "from scratch":
X = { ...
struct('a',1,'b',22,'c',[5 7 9]) ...
struct('a',4,'b',99,'c',[12 56 76]) ...
}
X = 1x2 cell array
{1x1 struct} {1x1 struct}
Now if all elements of X contain a scalar struct with the same fields, then you can make a struct array out of it:
Y = [X{:}]
Y = 1x2 struct array with fields:
a b c
If not all elements of X contain a struct or the fields are different between different structs in X, then you cannot do this and some additonal work is required to represent X in a table (since any field can be present or not in any struct in X). Therefore, I'll assume you have a struct array.
I'll also assume you have a string array of file names, one per element of X:
FileNames = "SoMe/fIlE\NaMe"+" "+(1:numel(Y))
FileNames = 1x2 string array
"SoMe/fIlE\NaMe 1" "SoMe/fIlE\NaMe 2"
Then one way to make an appropriate table is as follows:
thetable = cell2table(cellfun(@mat2str,permute(struct2cell(Y),[1 3 2]),'UniformOutput',false), ...
'RowNames',fieldnames(Y),'VariableNames',FileNames)
thetable = 3x2 table
SoMe/fIlE\NaMe 1 SoMe/fIlE\NaMe 2 ________________ ________________ a {'1' } {'4' } b {'22' } {'99' } c {'[5 7 9]'} {'[12 56 76]'}
John
John 2025 年 2 月 10 日
移動済み: Voss 2025 年 2 月 10 日
Thank you!
You truly are a MATLAB guru! I hope you can help again.
When I apply it to my script, please forgive me, the code to generate the data is quite long, so I can't show the entire script.
I displayed the final data which now is a 2 struct cell. fprintf class and size:
class of data: cell, size of data: [2 18]
display data showed:
data = 2×18 cell array
Columns 1 through 13
{2×1 double} {2×1 double} {2×1 double} {[4.5676]} {[3.1360e+04]} {[1.1183e+04]} {[0.2741]} {[2.8262]} {[0.0158]} {[0.7682]} {[0.9947]} {[0.9987]} {[2.0350]}
{2×1 double} {2×1 double} {2×1 double} {[4.5676]} {[2.1617e+04]} {[1.1674e+04]} {[0.9403]} {[3.8473]} {[0.0237]} {[0.8084]} {[0.9947]} {[0.9988]} {[2.0338]}
Columns 14 through 18
{[2.5434e+05]} {[5.8075e+11]} {[7.8644]} {[2.0091e+10]} {[10.0886]}
{[2.5434e+05]} {[3.1615e+11]} {[7.6420]} {[1.1234e+10]} {[ 9.8517]}
Display data{1} or data{2} showed each is a 'struct' of above.
The error is below:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in makeTable (line 49)
datastruct = [data{:}];
^^^^^^^
Voss
Voss 2025 年 2 月 10 日
There don't appear to be any structs in data, only numeric vectors (e.g., "2x1 double", 4.5676, etc.).
Walter Roberson
Walter Roberson 2025 年 2 月 10 日
What result are you expecting for combining a 2 x 1 double with a 1 x 1 double ?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

製品

リリース

R2024b

質問済み:

2025 年 2 月 5 日

コメント済み:

2025 年 2 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by