Nested table with adaptive numbers of columns
Show older comments
Hi please help if you can,
I am trying to create an adaptive table that looks something like this:

However both the number of test cases, and number of repeats can be 1,2,...n etc.
I have made an attempt at this using the following method:
output = inputdlg({'Enter number of test cases'},'Input',[1 35],{'4'});
n_testcase = str2double(output{1, 1}); %Number of test cases
pre = 'Case';
case_names = {};
for i = 1:n_testcase
case_names = [case_names;strcat([pre,num2str(i,'%01d')])];
end
case_names = case_names'; %Names of test cases
output = inputdlg({'Enter number of repeats per test case'},'Input',[1 35],{'3'});
n_repeats = str2double(output{1, 1}); %Number of repeats
pre = 'R';
repeat_names = {};
for i = 1:n_repeats
repeat_names = [repeat_names;strcat([pre,num2str(i,'%01d')])];
end
repeat_names = repeat_names'; %Names of repeats
for i = 1:n_testcase
tab(i) = {array2table(zeros(3,n_repeats), 'VariableNames', repeat_names(1:n_repeats))};
end
%Attempt 1
table1 = table(tab{1,1:n_testcase}, 'VariableNames', shortname(1:n_testcase));
table1.Properties.RowNames = {'Onset (°C)', 'Peak (°C)', 'Heat released (J/g)'}
%Attempt 2
table1 = table(tab(1,1),tab(1,2),tab(1,3),tab(1,4), 'VariableNames', shortname(1:n_testcase));
Attempt 1 is close and it is adaptive. However, I need to be able to access specific columns to asign values eg. Case1,R1 which this table doesnt allow me.
Attempt 2 allows me to select a column e.g.
table1.Case1{1,1}.R1
However it is not adaptive and also does not allow me to assign row names.
I would also ideally like to be able to write the table to a csv file.
I would be extremely grateful for any help.
Robin
Accepted Answer
More Answers (0)
Categories
Find more on Write Unit Tests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!