I'm having trouble running my script

1 次查看(过去 30 天)
I'm having trouble with the syntax of this code, specifically, declaring the variables & all syntax related to arrays/vectors. As you can see I'd like to use Names & Scores as arrays, but I don't exactly know how to do this with Matlab code. The code is below:
String Names[30];
Integer Scores[30,5];
String StudentName;
Float Sum;
Float Average;
Count = 0;
StudentName = input('Enter a students name; enter * when done: ');
while(StudentName ~= '*')
Names[Count] = StudentName;
disp('Enter 5 test scores for ' + Names[Count]);
Test = 0;
while(Test < 5)
input(Scores[Count,Test]);
Test = Test++;
end
Count = Count + 1;
StudentName = input('Enter a students name; enter * when done: ');
end
K = 0;
while(K <= Count - 1)
Sum = 0;
J = 0;
while(J < 5)
Sum = Sum + Scores[K,J];
J = J + 1;
end
Average = (Sum / 5);
disp(Names[K] + ': ' + Average);
K = K + 1;
end

回答(3 个)

John D'Errico
John D'Errico 2016-6-17
编辑:John D'Errico 2016-6-17
Of course, this is not even close to valid MATLAB syntax, with errors in perhaps half of the line of your code.
I think it is time to read the manual. Start to learn MATLAB, instead of trying to write in some made up language.
The major errors are things like you not understanding how to index arrays. [] is NOT correct. () is correct.
MALTAB indexing is 1 based, not zero based. So your indexing will fail in at least one place.
You don't declare arrays and variables as Strings, Floats, etc. as you have done. You don't declare any variables in MATLAB. (Ok, globals and persistent variables require something like a declaration.)
You don't test for equality of a string variable as you have done.
I could go on for a while like this.
READ THE MANUAL.

dpb
dpb 2016-6-17
Matlab doesn't have explicit typing; numeric things are double by default, you specify more exotic data structures such as cell arrays, structure, tables, etc., etc., etc., ... by their constructors.
So, for a start, simply remove the initial lines
String Names[30];
Integer Scores[30,5];
String StudentName;
Float Sum;
Float Average;
entirely, although you could preallocate some storage for the Scores array as
Scores=zeros(30,5);
Also, use the 's' optional argument on input to return the string...
StudentName = input('Enter a student''s name; enter * when done: ',s);
should get you started...

Shameer Parmar
Shameer Parmar 2016-6-17
Hello Jeremy,
try for this code...
clear all;
clc;
num = input('\nEnter total number of students = ');
for i = 1:num
Names{i,1} = input(['\nEnter Name of Student #',num2str(i),' in single inverted commas = ']);
Score{i} = input(['\nEnter the 5 scores of Student ',num2str(i),' in scare brackets = ']);
Average{i,1} = num2str(mean(Score{i}));
end
dataToDisplay = [Names Average];
disp('=================================')
disp('Data of Students are as follows:');
dataToDisplay
This is equivalent to your code...

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by