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));
newNo = zeros(length(uniqueNames), length(uniqueLists));
for i = 1:length(uniqueNames)
for j = 1:length(uniqueLists)
idx = strcmp(t.name, uniqueNames{i}) & strcmp(t.list, uniqueLists{j});
newCorners(i, j) = t.corners(idx);
The tables can be concatenated as:
data = [newCorners(:, 1), newNo(:, 1), newCorners(:, 2), newNo(:, 2), newCorners(:, 3), newNo(:, 3)];
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'});
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:
Thanks!