I'm having trouble running my script
1 view (last 30 days)
Show older comments
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
0 Comments
Answers (3)
John D'Errico
on 17 Jun 2016
Edited: John D'Errico
on 17 Jun 2016
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.
0 Comments
dpb
on 17 Jun 2016
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...
0 Comments
Shameer Parmar
on 17 Jun 2016
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...
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!