I can't figure out how to write in excel using if statements

조회 수: 2 (최근 30일)
Luke Sheehan
Luke Sheehan 2021년 3월 25일
편집: Cris LaPierre 2021년 3월 26일
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.

답변 (1개)

Cris LaPierre
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 = 1×5
1 2 3 4 5
a<3
ans = 1×5 logical array
1 1 0 0 0
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.

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by