import data and print test scores

9 visualizaciones (últimos 30 días)
Wal MathEngineering
Wal MathEngineering el 5 de Oct. de 2021
Respondida: Timothy el 5 de Oct. de 2021
Hi,
I made a mainscript and when run the code, it gives options. one of the options is to print the student test1,test2,and test3 from the excel file.
I wrote another script (as a function ) but struggle how to print only student test scores.
Here is what I have:
function y = question1(~,cells) % this is also included in the main scripts
% Print the grades of the student
y = 0;
name = 0;
for i = 1:size(cells,1)
if strcmp(cell2mat(cells(i,2)),name)
name = name + 1;
n= cell2mat(cells(i,2));
grade = cell2mat(cells(i,3));
end
end
fprintf( ' the grades of %s are \n',n,grade);
end

Respuestas (1)

Timothy
Timothy el 5 de Oct. de 2021
Check the help for fprintf (www.mathworks.com/help/matlab/ref/fprintf.html?searchHighlight=fprintf&s_tid=srchtitle#btf8xsy-1_sep_shared-formatSpec), you need to use placeholders of the correct type for each variable listed after the format specifier string. In your code above, %s seems right for n and you want either %u or %f for grade, depending of whether the grade is an integer or floating point variable.
Other issues that I see:
  • Name is a number, but you are using strcmp (string compare) to compare it to cell contents. If the cell contains a string like 'Test1', based on your description, you need to use name to construct a string to compare to the cell contents (e.g. test_name = sprintf('Test%u', name); , then use test_name in your strcmp call). You can also use strcmpi if the test names are not always lower or upper case. You also need to do this before you compare it to the cell contents. Your first pass through the for-loop compares to the value 0.
  • If the test names can appear out of sequential order, you will need to start the for-loop over each time to ensure that you check every element of the cell for the test name that you are looking for.
  • You use n in your fprintf line after the for-loop, but you change it each step through the for-loop. All the values written to n, except the last on, are not used by fprintf because of where the fprintf line is. If you want to print each test score, you need to move the fprintf line inside the for-loop.

Categorías

Más información sobre Write Unit Tests 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