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.