How to store user-generated text within a loop?

조회 수: 1 (최근 30일)
Caleb
Caleb 2014년 11월 13일
댓글: Evan 2014년 11월 14일
How would i write this code to run in a loop?
p='What is the name of a eng 1315 student? ';
n1=input(p,'s');
n2=input(p,'s');
n3=input(p,'s');
n4=input(p,'s');
n5=input(p,'s');
my objective involves importing data (which will be set of grades) from an excel doc then sorting and calculating the data for the entered students to be displayed in order from highest average to lowest. I would like to shorten this portion because extra credit will be given to the student with the shortest code. If running this in a loop is not possible I am open for any other suggestions to shorten this portion. Thanks.

채택된 답변

Evan
Evan 2014년 11월 13일
In order to store multiple strings, likely of different lengths, you will need to use a cell array. See here for help on writing data to a cell array.
It is certainly possible to do this in a loop. The input function will pause execution of the loop until the user enters data, and then it will move onto the next iteration.
Note that giving exact solutions to classwork is discouraged on this site, but users here will be very happy to help you with tips and partial solutions.
  댓글 수: 2
Caleb
Caleb 2014년 11월 14일
Thanks!
Evan
Evan 2014년 11월 14일
No problem. Good luck!

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

추가 답변 (1개)

Guillaume
Guillaume 2014년 11월 13일
Inside the loop store the result of input in a matrix, using the loop index as the matrix index.
n(i) = input(p, 's');
  댓글 수: 2
Evan
Evan 2014년 11월 13일
편집: Evan 2014년 11월 13일
This will not work if the strings are of different lengths (or, for that matter, if any of the strings are more than one element long).
Guillaume
Guillaume 2014년 11월 13일
Yes, indeed. Don't know why I was thinking Caleb was storing numbers.
As Evan says, use a cell array instead of a matrix.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by