MATLAB: Creating heading for table that changes depending on datainput

headingtable

grades_incl_final=[cellstr(data.StudentID) cellstr(data.Name) num2cell(grades) num2cell([computeFinalGrades(grades)]')];
array=[grades_incl_final];
table=array2table(array);
table.Properties.VariableNames = {'StudentID' 'StudentName' 'Assignment1' 'Assignment2' 'Assignment3' 'FinalGrade'}
disp(table)
Hi This code creates the table that you can see in the attached picture. My problem is that I want to be able to put more than three assignments into the table. In other words the headings should change depending on the amount of assignments in the 'data' file. If it is easier it is okay if the heading for the assignments are just 1,2,3,4… Can someone tell me how I can do this?

Best Answer

Assuming data is also a table:
t = [data(:, {'StudentID', 'Name'}), ... copy part of original table
array2table(grades, 'VariableNames', compose('Assignment%d', 1:size(grades, 2))), ... conversion of grades to table
table(computeFinalGrades(grades).', 'VariableNames', {'FinalGrade'})] %add final grade
Note that you will have to clear table so you can use the table function that you've clobbered with your table variable.
The bit where I generate the column name for the grades is
compose('Assignment%d', 1:size(grades, 2))