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

2 visualizaciones (últimos 30 días)
Luke Sheehan
Luke Sheehan el 25 de Mzo. de 2021
Editada: Cris LaPierre el 26 de Mzo. de 2021
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.

Respuestas (1)

Cris LaPierre
Cris LaPierre el 26 de Mzo. de 2021
Editada: Cris LaPierre el 26 de Mzo. de 2021
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.

Categorías

Más información sobre Data Import from MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by