Вы находитесь на странице: 1из 16

Chapter 2

- Input/Output
- Writing a program

Any program you write involves


Getting input
Processing
Generating output

Sources of input

Keyboard
Touch screen
Mouse
Microphone
USB Port (many devices)
Input file

Sources of output

Screen (text, graphs, images)


Output file
Speaker
USB port (many devices)

Sources of input

Keyboard
Touch screen
Mouse
Microphone
USB Port (Arduino)
Input file

Sources of output

Screen (text, graphs, images)


Speaker
USB port (Arduino)
Output file

Sources of input

Keyboard
Touch screen
Mouse
Microphone
USB Port (Arduino)
Input file

Sources of output

Screen (text, graphs, images)


Speaker
USB port (Arduino)
Output file

Getting input from user input


>> age = input(Please enter your age);

variable to be read

message to user

Reading a string using


input
>> name = input(Please enter your name,s);

Must be included if the input is a string (not a number)

Output Options disp


function
>> disp(1)

disp can be used to 1


>> disp('Paper length must me greater than 4')
display
a contstant

Paper length must me greater than 4

Scaler, matrix, string

the contents of a
variable without
printing the
variable name

>> paperL = 10
>> disp (paperL)
10

Output Options fprintf


function
fprintf gives high level of control over
how the output is displayed
fprintf(formatString,variables)
Example

Variable placeholder and formatter Newline special


character

>> fprintf(\n\nThe number of students is %d and the average grade is %5.2f \n


Great job class!\n',NumOfStudents,AverageGrade)

The number of students is 30 and the average grade was 88.50


Great job class!

Field formatter

More on number formatting (%f and


%d)
You can better format and align numbers by specifying
How many characters should be used to display the number
How many characters displayed after the decimal point
Example:
%6.2f

minmum width

number of digits after the decimal point

Spaces are appended to the right

>> temp = 100.733;


>>fprintf('%6.2f\n',temps)
100.73
>> temp = 99.5;
>>fprintf('%6.2f\n',temps)
99.50

Similarly, a number before the d in %d can be


used to align whole numbers

Useful special characters

Given the following two variables about the


number of drivers in two road safety statistical
study have the following values
NumOfDriversInSample1 = 897
NumOfDriversInSample2=1003
What is the output of the following commands?
>>fprintf(Sample\t Number\n',)
>>fprintf(1\t\t %4d\n',NumOfDriversInSample1)
>>fprintf(2\t\t %4d\n',NumOfDriversInSample2)
A)
Sample
1
2

Number
897
1003

C)
Sample Number
1 897
2 1003

B)
Sample
1
2

Number
897
1003

D)
Sample Number 1 897 2 1003

What is the output of the following


command?
>>fprintf(Part name is shaft\n',)

A) Part name is shaft


B) Part name is shaft
C) Error

Writing a program!!!!!
The m-file/script editor/debugger can
be used to write, run, and debug a
program
The editor can be launched by
selecting New Script on the HOME
tab

First example program

Getting user
input
Calculation
Printing output

Run the program by selecting Run on


the EDITOR tab or typing the name of
the m-file (volume) in the command

Вам также может понравиться