Display struct array as a table

I have a struct array of the form:
%--- Define stuct array 'sites' ---%
sites(1).code = 'LP5';
sites(1).name = 'Liverpool';
sites(1).comments = '';
sites(1).code = 'LDN';
sites(1).name = 'London';
sites(1).comments = 'Capital of the UK';
sites(1).code = 'MC1';
sites(1).name = 'Manchester';
sites(1).comments = '';
%--- And so on 'n' times ---%
I would like to be able to print this data out in the same format as a MATLAB table. Something that looks like (with text wrapping for long comments or names):
Code Name Comments
LP5 Liverpool
LDN London Capital of
the UK
MC1 Manchester
I know this is possible for regular none-array-like structs but not for struct arrays. with my current code:
function listSites(sites)
disp('--------------------');
for n = 1 : length(sites)
display(struct2table(sites(n)));
end
disp('--------------------');
end
I just get:
Variables:
name: 14×1 cell array of character vectors
code: 14×1 cell array of character vectors
comments: 14×1 cell array of character vectors

 采纳的回答

struct2table does that:
>> %--- Define stuct array 'sites' ---%
sites(1).code = 'LP5';
sites(1).name = 'Liverpool';
sites(1).comments = '';
sites(2).code = 'LDN';
sites(2).name = 'London';
sites(2).comments = 'Capital of the UK';
sites(3).code = 'MC1';
sites(3).name = 'Manchester';
sites(3).comments = '';
%--- And so on 'n' times ---%
>> struct2table(sites)
ans =
3×3 table
code name comments
_____ ____________ ___________________
'LP5' 'Liverpool' ''
'LDN' 'London' 'Capital of the UK'
'MC1' 'Manchester' ''

1 个评论

S G
S G 2019-5-2
编辑:S G 2019-5-2
Thanks, turns out I just didn't need the for loop!

请先登录,再进行评论。

更多回答(1 个)

Peter Perkins
Peter Perkins 2019-5-3
SG, two things:
1) Display for the table data type is intended to show the contents of the data container. It sounds almost like you are looking for a "pretty print" facility. If you're happy with table's display, that's great, but it's not really intended for pretty printing.
2) You should consider using a table instead of a struct array for those data. It looks like you have tabular data, and a table is likely to be more convenient.

4 个评论

Similar issue here. I am using struct2table(myStruct) to visualize what's going on inside a script. Is there any way to suppress the "ans =" in the command window when printing such a table? If I try something like
myTable = struct2table(myStruct);
fprintf('Here is my table: %s', myTable);
it complains— it seems like the various PRINTFs cannot treat a table as a string, so they can't be used this way. I think I've tried all the obvious format specifiers, but no luck so far.
Very minor nitpick, obviously, but I'm curious to learn if there's a cleaner way to handle this.
...Duh. I just realized
myTable = struct2table(myStruct);
fprintf('Here is my table:\n');
disp(myTable);
does what I need. Sorry for the query spam.
I hesitate to even suggest this, but
>> t = array2table(rand(5,3));
>> s = evalc('disp(t)');
>> fprintf('My table is:\n%s',s)
My table is:
Var1 Var2 Var3
________ _________ _______
0.079917 0.43643 0.54402
0.16308 0.0063417 0.38973
0.9033 0.58479 0.21583
0.78299 0.51848 0.38577
0.74668 0.06685 0.62686
Hi Peter,
Thanks for the additional idea. I never thought about any of the EVAL variants, probably out of old habit (I've seen them abused quite a few times to cause havoc via deliberately malformed input). But this works great, too. Thanks!

请先登录,再进行评论。

类别

帮助中心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