フィルターのクリア

Creating heading for table that changes depending on datainput

1 回表示 (過去 30 日間)
Håkon Haavik  Nystad
Håkon Haavik Nystad 2018 年 1 月 15 日
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?
  2 件のコメント
Guillaume
Guillaume 2018 年 1 月 15 日
  • Don't call your table table since that will prevent you from constructing tables the proper way with the table function.
  • There is no point constructing a cell array to then convert into a table. You can create the table directly.
  • What is the class of data? and what is the class of data.StudentID and data.Name ?
Håkon Haavik  Nystad
Håkon Haavik Nystad 2018 年 1 月 15 日
It is a csv file. An studentID and Name are headings in the table. So data.StudentID and data.Name are taken from the table/csv file

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2018 年 1 月 15 日
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))

その他の回答 (1 件)

Matt J
Matt J 2018 年 1 月 15 日
編集済み: Matt J 2018 年 1 月 15 日
headings=arrayfun(@(i) ['Assignment' num2str(i)],1:numberOfAssignments,'uni',0);
headings=[{'StudentID' 'StudentName'}, headings, {'FinalGrade'}];
grades_incl_final=[cellstr(data.StudentID) cellstr(data.Name) num2cell(grades) num2cell([computeFinalGrades(grades)]')];
array=[grades_incl_final];
T=array2table(array);
T.Properties.VariableNames = headings;
disp(T)
  1 件のコメント
Guillaume
Guillaume 2018 年 1 月 15 日
Since R2016b, matlab has had the compose function that would do that arrayfun bit a lot simpler:
compose('Assignment%d', 1:numberOfAssignments)
If I read the code correctly, then numberOfAssignments is size(grades, 2)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by