I can't figure out how to write in excel using if statements
2 ビュー (過去 30 日間)
古いコメントを表示
My questions will be under the code...
xlswrite("Grade_convert.xlsx","hi","sheet1",'B2')
Score = xlsread("Grade_convert.xlsx",'sheet2','B2:B11')
if Score < 57.1
xlswrite("Grade_convert.xlsx","F",'Sheet2','C2')
end
if Score >= 57.1
xlswrite("Grade_convert.xlsx","D-",'Sheet2','C2')
end
if Score>= 60.1
xlswrite("Grade_convert.xlsx","D",'Sheet2','C2')
end
if Score >= 63.1
xlswrite("Grade_convert.xlsx","D+",'Sheet2','C2')
end
if Score >= 67.1
xlswrite("Grade_convert.xlsx","C-",'Sheet2','C2')
end
if Score >= 70.1
xlswrite("Grade_convert.xlsx","C",'Sheet2','C2')
end
if Score >= 73.1
xlswrite("Grade_convert.xlsx","C+",'Sheet2','C2')
end
if Score >= 77.1
xlswrite("Grade_convert.xlsx","B-",'Sheet2','C2')
end
if Score >= 80.1
xlswrite("Grade_convert.xlsx","B",'Sheet2','C2')
end
if Score >= 83.1
xlswrite("Grade_convert.xlsx","B+",'Sheet2','C2')
end
if Score>=87.1
xlswrite("Grade_convert.xlsx","A-",'Sheet2','C2')
end
if Score >=90.1
xlswrite("Grade_convert.xlsx","A",'sheet2','C2')
end
This is for an assingment for a class, were supposed to convert the grades from numbers to letters using if statements and we put the letters on the second sheet a column over from the number grades. My issue the code I wrote won't write anything on the second sheet because the matrix "Score" is being evaluated as a whole instead of element by element or whatever the correct terminology is. I have attached the excel file however I had to use xlswrite because no data was showing up. Hopefully you can see what I'm trying to do and help me out because this assingment is already late.
0 件のコメント
回答 (1 件)
Cris LaPierre
2021 年 3 月 26 日
編集済み: Cris LaPierre
2021 年 3 月 26 日
Look into readtable and writetable. They are now preferred.
I think one of your issues is that that Score is a vecor of numbers. "If statements" expect a single logical result (or all the same). In your case, you are getting a different one for each score.
a=1:5
a<3
The result is that none of the if statements are true, so none of the conditional code is run.
Consider either checking each value separately, or use logical indexing to find all the vaues that meet a specific condition. You can learn more about logical arrays in Ch 12 of MATLAB Onramp.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Import from MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!