Here's the spreadsheet
Convert tablet to matrix
13 ビュー (過去 30 日間)
古いコメントを表示
Nils Hammarsten Brywe
2021 年 11 月 5 日
コメント済み: Nils Hammarsten Brywe
2021 年 11 月 14 日
My code:
filename='Excelspreadsheet.xlsx';
Data=readtable(filename)
7×3 table
Classes Hours Grades
{'CHM' } 3 {'C'}
{'Calc' } 4 {'A'}
{'CHM lab' } 2 {'F'}
{'College comp'} 4 {'D'}
{'ECAL' } 1 {'A'}
{'MHCS' } 1 {'B'}
{'EGE' } 1 {'A'}
A=4;B=3;C=2;D=1;F=0;
Problem:
What I'm trying to do is to either convert the characters under grades to something which allows me to convert this table into a matrix. Or my other solution would be to convert the grades to the corresponding numbers which I have written in the code. However, I can't get it to work.
3 件のコメント
採用された回答
Cris LaPierre
2021 年 11 月 5 日
編集済み: Cris LaPierre
2021 年 11 月 5 日
There may be an easier way, but here's what I came up with.
- Read in your grades as categoricals
- Rename the categories for grades from letters to numbers
- Convert your categorical grades to doubles
- Perform your calculation for gpa
% Step 1
opts = detectImportOptions('https://www.mathworks.com/matlabcentral/answers/uploaded_files/790884/Excelspreadsheet.xlsx');
opts = setvartype(opts,[1,3],'categorical');
T = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/790884/Excelspreadsheet.xlsx',opts)
% Step 2
T.Grades = renamecats(T.Grades,categories(T.Grades),["4","3","2","1","0"])
% Step 3
T.Grades = str2double(string(T.Grades))
% Step 4 (example calculation)
T.Sum = T.Hours + T.Grades
6 件のコメント
Cris LaPierre
2021 年 11 月 14 日
編集済み: Cris LaPierre
2021 年 11 月 14 日
As long as you know the grading system is A-F, you can define that in the code before the sheet is loaded. That is what I was trying to show you how to do. That code will work even when not all the categories appear in the spreadsheet.
Feel free to test it out. That's the only way I would be convinced it works. And it's faster than waiting for replies here.
その他の回答 (2 件)
Sulaymon Eshkabilov
2021 年 11 月 5 日
You can consider to convert it into cell array and matrix arrays, e.g.:
CLS = table2array(T(:,1)) % Cell Array
HRS = table2array(T(:, 2)) % Matrix Array
GRD = table2array(T(:,3)) % Cell Array
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!