Input function

The input function allows you to ask a user to type some sort of information into the program and to save that information into a variable that the program can process.

The Input Function

The input function is used to ask the user of the program (not the programmer) a question, and then wait for a typed response. The typed number is then returned as the result of the function, and should usually be stored in a variable:

        

        age = input('how old are you: ');
        % At this point, the variable: age, will contain
        % whatever value the user types
        

      

Reading Strings

By default, the input function expects to read a number, and if the user types: hello it will assume that hello is a variable containing a number. If you really want the string "hello" then you would have to type 'hello' (tick marks).

To tell a Matlab program to read a string of characters directly with out having to type the tick marks, you must use the 's' syntax as shown here:

        

        >>name = input('what is your name: ', 's');   % user types jim without tick marks
        name = jim
        % At this point, the variable: name, will contain
        % whatever value the user types (as a string of characters), in this case 'jim'


        >>name = input('what is your name: ');    % user types jim without tick marks
        ??? Error using ==> input
        Undefined function or variable 'jim'.

        >>name = input('what is your name: ');    % user types 'jim' with tick marks
        name = 
        jim
        
        
      

The input function is similar (but more user friendly) than the scanf function in C. Thus, in C, if you wish to receive input from the user of the program, you must use the scanf (or fgets) function.


Asking for input using a complex string

Sometimes you will have the situation where you need to ask the user for input, but the question that you want to ask needs to be built from data already stored in a variable. For example: The program has a variable called balance and the question you want to ask is: "You have 'balance' dollars in your bank account, how much would you like to withdraw?"

Unfortunately, you cannot use the formatted print options (such a %d) in an input statement, like you can in an fprintf statement. What you can do is combine the two.

        

          balance = 10534;

          % Incorrect:  input('Your balance is %d dollars, how much would you like to withdraw:',balance);

          % Correct:
          fprintf('Your balance is %d dollars, how much would you like to withdraw:',balance);
          withdrawal = input('');

        
      

By using a fprintf first, you can get any formatted output string you want. then by using the input with an EMPTY string, the user will only see the message and then blinking cursor waiting for input.

The moral of this story is: when the computer does not have a built in solution to a problem, you must use the available tools/functions to create another solution.


Back to Topics List