0

Inputting a number, e.g.

StudentNo = input('Please input your student number: ')

so that

StudentNo = 54456842

Putting this in and then making it into individual numbers

StudentNo = [5,4,4,5,6,8,4,2]

in order to find the avg or other stats from the digits without getting NaN?

1
  • Will the Student number always be a single digit or will it vary? Commented Oct 17, 2016 at 13:44

3 Answers 3

3

You can break it up into digits with the following approach

str2num(num2str(StudentNo).')

This first converts StudentNo to a string, then takes the transpose such that each character is on it's own line, and then we call str2num to convert each row to a separate number.

Another option would be to convert to a string and then subtract off the ASCII value of '0' to convert each character to a numeric digit

num2str(StudentNo) - '0'
Sign up to request clarification or add additional context in comments.

Comments

0

Suever's approach is a neat way to do this. However, if you don't need the actual number as a number, and you only need the separate digits, then you can specify directly that you want the numbers as a string in the input-call.

StudentNo = input('Please input student number: ', 's')

Here, the 's' specifies that the input is a string. This way you can input the StudentNo without having apostrophes.

StudentNo = input('Please input student number: ', 's')
Please input student number: 54456842
StudentNo =
54456842

You can now use any of the two approaches in Suever's answer to convert this to separate numbers. Another option is to simply do the subtraction by '0' in the call itself:

StudentNo = input('Please input student number: ', 's')-'0'
Please input student number: 54456842
StudentNo =
     5     4     4     5     6     8     4     2

Since this is a bit hard to read, you can also have this as an anonymous function:

stid = @() input('Please input student number: ', 's')-'0';

And call it like this:

stid()
Please input student number: 123456
ans =
     1     2     3     4     5     6

Comments

0

Input captures a single number. It would be hard to know how many digits each of the student numbers is so I would suggest getting them one by one:

StudentNo = input('Please input your student number one by one (end with -1): ');
while StudentNo(end)~= -1
    StudentNo = [StudentNo, input('Please input the next student number (end with -1): ')];
end
% remove the -1
StudentNo = StudentNo(1:end-1);

EDIT

Based on @StewieGriffin: comment if you have multi-digit student number you could just do this:

StudentNo = input('Please input the next student number: ')];
[1 2 12 13]

This will detect each number as separate entry in a vector of your student numbers.

2 Comments

Suever's approach handles the variable number of digits quite nicely, so I don't think that's a big issue. If however, you really want to input one by one, it would probably be easier to just input them all inside brackets: [1 2 3 4], thus avoiding the while loop.
The problem is with multi-digit student numbers, as in: [1 2 12 45] which would be converted into [1 2 1 2 4 5]. I did not know that input can actually detect [] properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.