1 / 22

Computer Programming and Problem Solving Input

Input/Output and Plotting Basics. Material from Chapter 2.6-2.7 and 2.11Input deals with things a user tells the computerOutput deals with things the computer tells the user. Keyboard Input. MATLAB allows you to prompt" for input interactivelyThis is through the input function:input(prompt_

leala
Télécharger la présentation

Computer Programming and Problem Solving Input

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. CS 101 – 010 Lecturer: Daniel J. Boston GITC 2315-C wednesday friday 1:00-2:30pm 2:30-4pm Computer Programming and Problem Solving Input/Output and Plotting Basics

    2. Input/Output and Plotting Basics Material from Chapter 2.6-2.7 and 2.11 Input deals with things a user tells the computer Output deals with things the computer tells the user

    3. Keyboard Input MATLAB allows you to “prompt” for input interactively This is through the input function: input(prompt_string) input(prompt_string, options_string) prompt_string tells MATLAB what text to display while waiting for a user to type something options_string lets you specify how MATLAB should interpret the input Use ‘s’ to indicate the input is a string

    4. Keyboard Input Try it out: user_name = input(‘What is your name? \n’, ‘s’) user_age = input(‘What is your age? \n’)

    5. Controlling Output MATLAB gives you a number of ways to control how output is displayed These controls range from simple to complex Note that changing how numbers are displayed does not change the precision of the underlying computations To change the default precision of echoed numbers in the Command Window, use the format command

    6. format Options

    7. disp function The disp function outputs array values to the Command Window This is often used to display strings (which are character arrays) Quickly convert numbers to strings by using num2str or int2str Example: str = [‘pi:’ num2str(pi) ‘ pi rounded:’ int2str(pi)]; disp(str)

    8. fprintf function fprintf can be used to control how output is displayed fprintf(format,data) fprintf(format,data1,data2, …) format is a string specifying how to display the data data, datax are variables or values (either scalar or array) Example: fprintf(‘pi: %d pi rounded: %1.0f \n’,pi,pi)

    9. fprintf function fprintf is not perfect; it only shows the “real” portion of complex numbers num2str with disp will show both real and complex parts of a number If you are displaying complex numbers, use disp

    10. fprintf function The %d characters in the format string are called “conversion characters” Each conversion character is a data place-holder in the format string Each conversion character has a number of optional formatting parameters These parameters go between the % and the conversion character (d, f, i, etc.)

    11. fprintf function

More Related