1 / 16

Chapter 7 Review: User-Controlled Input and Output

Chapter 7 Review: User-Controlled Input and Output. Introduction to MATLAB 7 Engineering 161. Introduction to Chapter 7. In this chapter we will learn how to write programs that interact with the user through input and output statements.

more
Download Presentation

Chapter 7 Review: User-Controlled Input and Output

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7 Review: User-Controlled Input and Output Introduction to MATLAB 7 Engineering 161

  2. Introduction to Chapter 7 • In this chapter we will learn how to write programs that interact with the user through input and output statements. • The input command pauses a program and prompts the user for input, then the program continues from that point. • The disp and fprintf commands provide formatted output to the Command Window.

  3. Chapter 7 Results • Write programs that interact with the user. • Gain some familiarity with importing and exporting data files to/from other applications. • Practice using input, disp, fprintf, and ginput commands.

  4. Input/Output • The following MATLAB built in functions allow the user to interact with a MATLAB program • “input” command causes the program to stop and prompts the user for input, • “disp” and “fprintf” commands cause output to be generated in the Command Window • “pause” command causes the program to stop until a key is entered which lets the program continue (useful to view intermediate results)

  5. Input/Output II • The input command • step_size = input (‘Enter Step Size’); When the input command is encountered by MATLAB, the program ceases execution, the character string in the parentheses is output to the Command Window. When the user enters (in this case a value for the variable step_size) and hits enter, the variable step_size is assigned the value input by the user and the program continues execution from the point where it had stopped. • A = input (‘Enter values for matrix A’) would allow the user to enter, for example, [1,2,3;4,5,6]. Upon hitting the enter key, A becomes a 2 X 3 matrix with values as given by the user.

  6. Input/Output II continued • You can input a string of characters using the input command as well as numbers; x = input(‘Enter your name in single quotes ‘); You enter ‘Sarah’ x is then assigned the string ‘Sarah’ • A second way to enter a string using the input command, x = input(‘Enter your name ‘, ‘s’); Your enter Ralph. x is then assigned the name Ralph

  7. Input/Output III • The disp command • The disp command looks like disp (x) where x can be a variable, x can be a character string surrounded by single quotes or it can be a variable name representing a matrix. • >>disp(‘This is a string of characters’) causes MATLAB to output this string in the Command Window. • >>disp(x) where x = [1 2 3 4 5] causes MATLAB to output the values of x in the Command Window. • >> disp without an argument outputs a blank line Use the disp command to label table columns and create blank lines to format your output in the Command Window. fprintf is a more powerful printing command to format output.

  8. Input/Output III continued • Combining input and disp, an example, name = input(‘Enter your name ‘, ‘s’); (you enter Sam) disp([‘Hi ‘, name]); would produce the following; Hi Sam in the Command Window. (Note the square brackets here.)

  9. Input/Output III continued • Suppose you want to output a string followed by a set of values for a variable, for example, x = [1 2 3 4 5]; disp([‘The list of values of x are: ‘, num2str(x)]) this would produce the line in the Command Window The list of values of x are: 1 2 3 4 5

  10. Input/Output IV • The “fprintf” command • This is the general purpose printing command that gives you more control over how you want your data formatted and output. • The format for this command is >>fprintf (format_string, variable1, variable2, . . . ) • The format_string contains one or more place holders (%) where the values of the variables are to be placed in the output string. • The characters immediately following each % tell MATLAB how to print the variable, for example

  11. Input/Output IV continued • %8.2f tells MATLAB to print the variable in a field of 8 characters with 2 decimal places using point fixed formatting. • %10.4e tells MATLAB to print the variable in a field of 10 characters with 4 decimal places using exponential notation, _3.2561e15 might be printed right justified in the field of 10. • Table 7.1 on page 247 of our text specifies several types allowed in MATLAB. Use help fprintf to see the complete list of options. • Consider the case where two variables are to be printed in a single line, say x1 = 12.2 and x2 = 1.1, >>fprintf(‘x1 has the value %4.1f and x2 has the value %3.1f \n’, x1,x2) would yield the following line in the Command Window, x1 has the value 12.2 and x2 has the value 1.1 The \n at the end of format string tells MATLAB to issue a carriage return and line feed at the end of the line. See Table 7.2 for printing control characters. Without it, the next print statement would continue to print from the end of this line, not on a new line.

  12. Input/Output IV continued • What happens if the variable in the fprintf command is a matrix? • First, fprintf prints by columns, i.e., the first column is printed across the page followed by the second column, and so on. • In the format_string, there needs to be one format type (%) for each element in the columns of the matrix, i.e., for each row in the matrix. • The fprintf statement will be executed multiple times by MATLAB, once for each column, resulting in a table being generated in the output displayed in the Command window. • In general, you need to end the format_string with \n so that each column of the matrix is printed on a new line.

  13. Input/Output V • Formatted Output, like the gtext command the sprintf command allows you to add text to a graph. Whereas the gtext command specifies the content of the text message and can not be changed, the sprintf command allows you to change the content of the text when the program runs, hence the text is alterable based upon the calculations when the program runs. • a = sprintf(‘Some example output is %4.2f \n’, data); • Here when the program runs a is assigned the string including the data item properly formatted. • text(10, data, a) then prints the formatted string at location (10,a) on the graph.

  14. Graphical Input • This is a very nice feature that lets you interact with a graph that you have just produced to pick of values (x,y) that you want your program to use for further calculations. Consider, x = 5:30; y = x.^2 – 40*x + 400; plot(x,y); axis([5,30, -50, 250]); % makes is easier to pick off [a,b] = ginput; When ginput is encountered, the figure window pops up with a set of crosshairs. You position the crosshairs over the graph where you want to select its value and hit enter. a,b then take on those values, and the program continues.

  15. Reading and Writing Data from files • Our book gives us a short introduction to importing and export data files with other applications. • Start by entering the command doc fileformats to get a better understanding of what’s possible and how to do it. • As you use MATLAB more and more you will likely want to exchange data with Excel for example, import text files, import wave files, and so on. • Our book just gets you started.

  16. Chapter 7 Assignments • The following assignments let you use the input and output statements. • Assignments 7.5 and 7.6, 7.9, 7.12, 7.13 and 7.15.

More Related