Skip to main content
Logo image

Section 5.4 Input to Script File

There are several ways to assign values to variables that are used in a script file:
  1. Edit the script file
  2. Assign values to the variables in the Command Window and then run the script as demonstrated below. In this example, the script is named doStuff.m. If this script processes data from a file whose name is stored in the variable filename then one way to change from which file the script reads is to assign a filename in the Command Window before running the script.
    >> filename = 'numbers.txt'
    >> doStuff
    >> filename = 'more_numbers.txt'
    >> doStuff
    
  3. Ask the user for input by using the input command:
    >> variable_name = input('prompt string');
    
    This function is like a combination of the printf() and scanf() commands that we use in C. Take a look:
    >> nfiles = input('Enter number of files: ')
    
    Enter number of files: 6
    
    nfiles =
        6
    

Subsection 5.4.1 Inputting Strings

By default, the input command expects a number to be entered. If you wish the user to enter characters or strings (arrays of characters) you need to tell the input function with the optional parameter ’s’. Check out the following syntax:
>> variable_name = input('prompt string','s')
Here’s an example:
>> fname = input('Enter filename: ','s')
Enter filename: numbers.txt
fname =
    numbers.txt

Activity 5.2.

The wind chill temperature, \(T_{wc}\text{,}\) is the air temperature felt on exposed skin due to wind. It is calculated by
\begin{equation*} T_{wc} = 35.74 + 0.6215T - 35.75v^{0.16} + 0.4275Tv^{0.16} \end{equation*}
where \(T\) is the temperature in degrees Fahrenheit and \(v\) is the wind speed in mi/h.
Write a MATLAB program in a script file that asks the user to enter values for \(T\) and \(v\) and then calculates the wind chill temperature and displays the message "The wind chill temperature is …".
In order to output the message to the user you can use the disp() command (try it out!). We’ll learn other ways to output data soon.
Please paste your code into the textbox: