Table formatting to combine the variable names as sub header

2 visualizzazioni (ultimi 30 giorni)
Hallo everyone,
I am trying to get my table in particular format, but i am unable to figure out the best possible way.
here is a small example: Consider a table 't' with the following contents:
name = {'square';'circle';'triangle';'square';'circle'};
list = {'A';'B';'A';'C';'C'};
corners = [4;0;3;4;0];
no = [1;4;10;23;2];
t = table(name,list,corners,no)
t =
name list corners no
__________ ____ _______ __
'square' 'A' 4 1
'circle' 'B' 0 4
'triangle' 'A' 3 10
'square' 'C' 4 23
'circle' 'C' 0 2
from this i need to arrive at a table structure which looks like this:
t =
name A B C
__________ corners no_ corners no corners no
'square' 4 1 0 0 4 23
'circle' 0 0 0 4 0 2
'triangle' 3 10 0 0 0 0
I tried using unstack but its not compatible for this i guess, so any other suggestions?
Thank u all in advance

Risposte (1)

Rajanya
Rajanya il 24 Dic 2024
Hi @ppp,
You can create two separate tables (one for storing ‘corners’ data and one for storing ‘no’ data), entries initialised with zeros, having the number of rows equal to the number of unique names and the number of columns equal to the number of unique list items. The original table can then be iterated and the ‘corners’ and ‘no’ values can be extracted and put in the matching entries of the new ‘corners’ and ‘no’ tables.
Here, going with the example given above for demonstration purposes, I have extracted the unique names and the unique list items, and I iterate over them to find any matching entries with the same name and list item value in the original table ‘t’. Using logical indexing, all the ‘corners’ and ‘no’ values for those matching entries are then put to the new tables accordingly.
newCorners = zeros(length(uniqueNames),length(uniqueLists)); %the new 'corners' table
newNo = zeros(length(uniqueNames), length(uniqueLists)); %the new 'no' table
for i = 1:length(uniqueNames) %uniqueNames would be {'circle','square','triangle'}
for j = 1:length(uniqueLists) %uniqueLists would be {'A','B','C'}
% Find the corresponding rows in the original table where the name is uniqueNames{i} and the list value is uniqueLists{j}
idx = strcmp(t.name, uniqueNames{i}) & strcmp(t.list, uniqueLists{j}); %logical index array
% If there is a match, populate the corresponding cell entries
if any(idx)
newCorners(i, j) = t.corners(idx);
newNo(i, j) = t.no(idx);
end
end
end
The tables can be concatenated as:
data = [newCorners(:, 1), newNo(:, 1), newCorners(:, 2), newNo(:, 2), newCorners(:, 3), newNo(:, 3)];
%first col is A, second is B and third is C for both newCorners and newNo
To preserve the hierarchical structure of the header and to add the visual formatting, one way can be to use ‘uitable’. Using ‘data’ and with proper position adjustments and setting of header names, the following table is obtained. This is just for visual formatting though; the underlying structure of ‘data’ would not have the headers to it.
MATLAB’s ‘table’ structure does not directly support the hierarchy of headers like above, so a workaround to achieve this without using ‘uitable’ could be to just name the columns as ‘corners_A, ‘no_A’ and so on. For example,
t_combined = table(uniqueNames,newCorners(:, 1), newNo(:, 1), ...
newCorners(:, 2), newNo(:, 2),newCorners(:, 3), newNo(:, 3), ...
'VariableNames', {'name', 'corners_A', 'no_A', 'corners_B', 'no_B', 'corners_C', 'no_C'});
%the concatenation of newCorners and newNo can be done here itself.
This results in a structure like:
To learn more about ‘uitable’ and its usages, you can refer to its respective documentation page by executing the following command in the MATLAB Command Window:
doc uitable
Thanks!

Categorie

Scopri di più su Tables in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by