520 likes | 536 Views
Learn how to prompt users for input, create formatted output, and utilize graphical techniques for program input in MATLAB. Explore user-defined input, output options using disp and fprintf functions, formatted output with fprintf, and cell mode. Discover graphical input using ginput function to get ordered pairs effortlessly.
E N D
User Controlled Input and Output Chapter 7
In this chapter we’ll learn how to… • Prompt the user to enter information into an M-file program • Create output using the disp function • Create formated output • Use graphical techiniques to provide program input • Use cell mode
Section 7.1User Defined Input • To this point we have “hard coded” the values of variables into our M-file programs • The input function allows us to prompt the user to enter a value
The input function is used in an M-file program to prompt the user to enter a value The prompt is displayed in the command window
Input accepts a variety of data • Scalars • Matrices • enter inside square brackets • Character strings • enter inside single quotes • Or… specify string input with ‘s’
Run this program twice – once with numeric input and once with character input Matrix input Character input
Section 7.2Output Options • Enter the name of a variable • Use the disp function • Use the fprintf function
disp The display (disp) function can be used to display the contents of a matrix without printing the matrix name
Strings are really arrays of character information The result is a character array
You can combine disp functions to create meaningful output from an M-file program, but the result of each disp function is on a separate line.
Since the disp function only takes one input, you must combine arrays to make more complicated output • Use the num2str(x) function to change numeric information to a string disp(['The values in the x array are: ' num2str(x)])
Although these characters look like numbers, they are interpreted by the computer as part of a character array – they no longer have any numeric meaning Notice that the ans matrix is listed as a character array
Hint • If you want to include an apostrophe in a string, you need to enter the apostrophe twice. • If you don’t, Matlab thinks the apostrophe terminates the string. For example: disp('The moon''s gravity is 1/6th that of the earth')
This Matlab program mimics a conversation, by using the input and disp functions. Watch the interactions as it runs in the next slide
Click on the slide to play the movie – Use the down arrow to advance to the next slide
Formatted Output • fprintf gives you more control over your output than the disp function • You can combine text and numbers • You can control how many digits to display, and their position
fprintf Arguments • format-string • includes place holders and formating information for numbers • list of matrices
fprintf • The fprintf command is more flexible than the disp command, and allows you to put both variables and text onto the same line
8 total spaces2 after the decimal pointfloating point format Place holder for your variable value Variable
X is a matrix /n is a carriage return
This example was created on the student edition of Matlab – Notice that the prompt is EDU Despite the way it looks, the computer always considers a matrix as one big list, working down one column at a time
Hint • One of the most common mistakes new programmers make is to forget to include the f in the placeholder sequence. The fprintf function won’t work, but no error message is returned either.
Hint • If you want to include a percentage sign in an fprintf statement, you need to enter the % twice. If you don’t, Matlab thinks the % is a placeholder for data. For example: fprintf('The interest rate is %5.2f %% \n', 5) results in: The interest rate is 5.00 %
Here’s another example that uses the input, disp and fprintf functions • Write a program in an M-file that creates a table of degrees to radians • Prompt the user to enter the table starting value, an increment between values, and a final value.
Graphical Input • You can enter ordered pairs of x and y values, by picking them off a graph • ginput
ginput • [x,y] = ginput • Retrieves a set of ordered pairs from the graph everytime the enter key is struck • [x,y]=ginput(n) • Retrieves n ordered pairs
Floating Crosshair When the ginput function is executed, a floating crosshair appears on the graph. Each time the enter key is struck, Matlab picks the corresponding points off the graph
Section 7.4Cell Mode • Cell Mode is new to Matlab 7 • It’s a utility that allows the user to divide an M-file program into sections, called cells • Each cell can be executed one at at time
To activate Cell Mode Select Cell -> Enable Cell, from the Editing Window menu bar
Cells are created with cell dividers, %% Cell Tool bar Cell tool bar
Evaluate Current Cell Evaluate Cell and Advance Evaluate the entire file Show Cell Titles Create Cell Dividers Divide and multiply value Increment and decrement value Save and publish to HTML
html report created from the cell mode From the file menu, you can also chose to create reports in both Word and Powerpoint formats
Section 7.5Reading and Writing Data from Files • Some common types of data files are • dat • txt • xls • jpg
Import Wizard • Use the import wizard to determine the data type and to suggest ways to represent the data • Launch from the file menu • From the uiimport funtion • uiimport(‘filename.extension’)
Command line approaches • The import wizard requires interaction with the user • Matlab includes a series of import functions that automatically import the data • You need to know what the data type is before you can use these
Import functions • To import a .mat or .dat file, use the load command • Other functions can be found if you type • doc fileformats • For example, wavread can be used for wav files • xlsread can be used for Excel files
Exporting Data • Use the save function for .mat or .dat files • Use specialized functions for other file types • For example • xlswrite for Excel files
Use the file menu to save a file interactively, using a variety of file formats
Summary • The input function allows the user to interact with the program by entering data at a prompt • The disp function displays information in the command window, and is especially useful for titles and labels
Summary - fprintf • The fprintf function is more versatile than the disp function and allows you to combine text and numeric information in formated output
Summary - ginput • The graphical input function allows the user to pick information off a graph, and store it as a series of order x-y pairs
Summary – Cell Mode • Cell mode allows the programmer to group M-file code into sections, and to run each section individually • It includes options to publish M-files in html, Word or Powerpoint formats • The cell toolbar allows the user to interactively change parameter values each time the code is executed