1 / 68

User-Controlled Input and Output Chapter 7

User-Controlled Input and Output Chapter 7. Objectives. After studying this chapter you should be able to: Prompt the user for input to an M-file program Create output using the disp function Create formatted output fprintf sprintf Use graphical techniques to provide program input

alissad
Download Presentation

User-Controlled Input and Output Chapter 7

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. User-Controlled Input and OutputChapter 7

  2. Objectives After studying this chapter you should be able to: • Prompt the user for input to an M-file program • Create output using the disp function • Create formatted output • fprintf • sprintf • Use graphical techniques to provide program input • Use cell mode to modify and run M-file programs

  3. 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

  4. 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

  5. Input accepts a variety of data • Scalars • Matrices • enter inside square brackets • Character strings • enter inside single quotes • Or… specify string input with ‘s’

  6. Run this program twice more – once with numeric input and once with character input Matrix input Character input

  7. Indicates that the input should be interpreted as a string

  8. Section 7.2Output Options • Enter the name of a variable • Use the disp function • Use the fprintf function • Use the sprintf function

  9. disp The display (disp) function can be used to display the contents of a matrix without printing the matrix name

  10. The disp function can also be used to display a string

  11. Strings are really arrays of character information The result is a character array

  12. 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.

  13. 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)])

  14. 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

  15. 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')

  16. This MATLAB program mimics a conversation, by using the input and disp functions. Watch the interactions as it runs in the next slide

  17. 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

  18. fprintf Arguments • format-string • includes place holders and formating information for numbers • list of matrices

  19. fprintf • The fprintf command is more flexible than the disp command, and allows you to put both variables and text onto the same line

  20. 8 total spaces2 after the decimal pointfloating point format Place holder for your variable value Variable

  21. You can also use exponential format

  22. X is a matrix /n is a carriage return

  23. 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

  24. 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.

  25. 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 %

  26. 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.

  27. Input

  28. Sending formatted output to a file • First create and open an empty output file, and assign it a file identifier • Add the file identifier to the first field in the fprintf function

  29. flag that identifies this as a file to which we can write File identifierany legitimate MATLAB name will do File name Create and open an output file and assign it a file identifier

  30. The result in the command window is the number of characters in the output string The output string is sent to the specified file Add the file identifier to the first field in the fprintf function

  31. Double clicking on the file name in the current directory window causes an editing window to open – which displays the contents of the file

  32. sprintf • The sprintf function is similar to fprintf • Instead of just sending the output to the command window, sprintf assigns the output a name, and then sends it to the command window

  33. The result from fprintf is a character count The result from sprintf is the actual string Why is this useful? sprintf could be used to specify a string that is then used in a graph annotation

  34. 7.3 Graphical Input • You can enter ordered pairs of x and y values, by picking them off a graph • ginput

  35. 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

  36. 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

  37. Section 7.4More Cell Mode Features • Publish • Value Manipulation

  38. Publish Icon M-files that use cell mode, such as this one used to solve homework problems from chapter 5, can be published using MATLAB’s publish feature

  39. Results published in HTML

  40. To publish in a different format, such as Word or PowerPoint, change the publish configuration From the file menu select File→Publish Configuration for…

  41. Value Manipulation Tools Increment and decrement value Divide and multiply value This is the number that is changed, because of the cursor placement

  42. Section 7.5Reading and Writing Data from Files • Some common types of data files are • dat • txt • xls • jpg

  43. 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’)

More Related