Lesson 06: User-Controlled Input and Output

Objective

  • Learn how to prompt the user for input to a script
  • Learn how to create output with the disp function
  • Learn how to create tables using the table function
  • Learn how to create formatted output using fprintf
  • Learn how to create formatted output for use in other function with the sprintf function

Background

The simplest type of MATLAB program is called a script. A script is a file that contains multiple sequential lines of MATLAB commands and function calls. You can run a script by typing its name at the command line.

There are two ways to create a script file

  • Click New Script icon to create a new script file
  • Or, in the command windows, use the edit command

    >> edit myScriptName

    This command opens a blank file named myScriptName.m in the current workspace folder.

To run the script, type its name at the command line:

>> myScriptName

In this lab, you have to implement the code using Script file.

Questions

  1. This problem requires you to generate temperature conversion tables. Use the following equations, which describe the relationships between temperatures in degrees Fahrenheit (TF), degrees Celsius (TC), kelvins (TK), and degrees Rankine (TR), respectively:
    \[{T_F} = {T_R} - 459.67^\circ R\]
    \[{T_F} = {9 \over 5}{T_C} + 32^\circ F\]
    \[{T_R} = {9 \over 5}{T_K}\]
    You will need to rearrange these expressions to solve some of the problems.
    1. Generate a table of conversions from Fahrenheit to Kelvin for values from 0°F to 200°F. Allow the user to enter the increments in degrees F between lines. Use disp and fprintf to create a table with a title, column headings, and appropriate spacing.
    2. Generate a table of conversions from Celsius to Rankine. Allow the user to enter the starting temperature and the increment between lines. Print 25 lines in the table. Use disp and fprintf to create a table with a title, column headings, and appropriate spacing.
    3. Generate a table of conversions from Celsius to Fahrenheit. Allow the user to enter the starting temperature, the increment between lines, and the number of lines for the table. Use disp and fprintf to create a table with a title, column headings, and appropriate spacing.

© 2024 Air Supply Information Center (Air Supply BBS)