MATLAB - 如何在数组

时间:2015-10-22 16:36:24

标签: matlab

例如,我有一组放在数组中的测试分数。在MATLAB上,如何显示指示数组的第一个值为“Student 1”的文本,第二个值为“Student 2”等等,无论我在其中放置多少个测试分数,都会有效?

1 个答案:

答案 0 :(得分:1)

这可能会有所帮助:

grades = round(unifrnd(1, 10, [100,1]));
for istud = 1: length(grades)
    fprintf ('student %i \t grade: %i\n', istud, grades(istud))
end

你得到的是

student 1    grade: 5
student 2    grade: 9
student 3    grade: 9
student 4    grade: 5
student 5    grade: 7
student 6    grade: 8
student 7    grade: 6
student 8    grade: 7
student 9    grade: 8
student 10   grade: 6
student 11   grade: 3
student 12   grade: 7

等等

如果您想将其写入文件而不仅仅是屏幕使用

fid   = fopen('grades.txt','w');
for istud = 1: length(grades)
    fprintf (fid,'student %i \t grade: %i\n', istud, grades(istud))
end
fclose (fid)