I need help with basic input/output commands in a script file

조회 수: 5 (최근 30일)
My problem reads this:
Write a MATLAB program in a script file that calculate the average, standard deviation, and median of a list of grades as well as the number of grades on the list. The program asks the user (input command) to enter the grades as elements of a vector. The program then calculates the required quantities using MATLAB’s built-in functions length, mean, std, and median. The results are displayed in the Command Window in the following format:
  • “There are XX grades.” where XX is the numerical value.
  • “The average grade is XX.” where XX is the numerical value.
  • “The standard deviation is XX.” where XX is the numerical value.
  • “The median deviation is XX.” where XX is the numerical value.
Execute the program and enter the following grades: 81, 65, 61, 78, 94, 80, 65, 76, 77, 95, 82, 49, and 75.
*This is my current script <file:*> http://i.imgur.com/D76ZqmO.png?1
What I'm getting from this in the command window does not make sense... it looks like this: (after running the script file)
Enter grades: 81 65 61 78 94 80 65 76 77 95 82 49 75
There are 38 grades.
The average grade is 46.89.
The standard deviation is 10.46.
The median deviation is 53.00.EDU>>
Now I know there obviously aren't 38 grades, and the others are wrong as well. I've tried several different ways and have read up on everything in the tutorial, manual, and online and can't find what I'm doing wrong. Any help on this is appreciated. Thanks!

채택된 답변

Walter Roberson
Walter Roberson 2013년 3월 3일
You are reading the grades as a string, but then you are treating the string as if were a vector of numbers. Either read the grades as a number (no 's' option) or convert the string to numeric.
  댓글 수: 4
Walter Roberson
Walter Roberson 2013년 3월 4일
Whatever happened to testing?
>> S=input('Enter grades: ')
Enter grades: 1 5 9 17
1 5 9 17
|
Error: Unexpected MATLAB expression.
>> S=input('Enter grades: ')
Enter grades: 1,5,9,17
Error: Unexpected MATLAB expression.
Enter grades: [1 5 9 17]
S =
1 5 9 17
If you want the user to be able to enter them without the [] then input a string and convert it.
>> str2double(regexp('1 5 9 17','\s+','split'))
ans =
1 5 9 17
>> sscanf('1 5 9 17', '%f')
ans =
1
5
9
17
>> str2num('1 5 9 17')
ans =
1 5 9 17
Lemmiwinks
Lemmiwinks 2013년 3월 4일
Ok thank you. I just needed to have the user enter the brackets (both of them). The tests I had done were with a bracket included in the prompt, only requiring the input of the closing bracket from the user, but I forget that that is only a prompt and doesn't actually affect the data input. Thank you for your help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Holidays / Seasons에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by