SlideShare a Scribd company logo
1 of 32
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system,
or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and
Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
User Controlled Input
and Output
Chapter 7
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
In this chapter we’ll learn
how to…
• Prompt the user to enter data
into an M-file program
• Make your output look better
• Use cell mode (debug feature)
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
Section 7.2
Output Options
• Enter the name of a variable in
the Command Window
• Use the disp() function
• Use the sprintf() (or fprintf())
function
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
disp
The display
(disp) function
can be used
to display the
contents of a
matrix without
printing the
matrix name
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
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.
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
Since the disp function only takes one
input, you must combine arrays to make
more complicated output
• One way to have a single-line output
is to use the num2str(x) function to
change numeric information to a
string
x=[35 2]
disp(['The values in the x array are: ' num2str(x)])
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
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
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
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')
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
This MATLAB program mimics a
conversation, by using the input and
disp functions.
Watch the interactions as it runs in the
next slide
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
Formatted Output
• sprintf 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
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
sprintf vs fprintf
sprintf is designed to generate a
character string
fprintf is designed to write to a file
Syntax for both functions is very similar
We will primarily use sprintf in MA 116
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
sprintf vs fprintf
We can use sprintf to generate a title
title(sprintf('%d widgets', widgets))
(this won't work with fprintf)
The book uses fprintf(), but they work nearly the
same.
sprintf('%d widgets', widgets)
fprintf('%d widgets', widgets)
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
sprintf Arguments
sprintf('%d widgets', widgets)
• format-string
• includes place holders and
formatting information for numbers
• list of variables
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
sprintf
• The sprintf command is more
flexible than the disp command,
and allows you to put both
variables and text onto the same
line
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
Place holder for
your variable value
Variable
8 total spaces
2 after the decimal point
floating point format
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
You can also use exponential
format
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
X is a matrix
n is a carriage return
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
sprintf
xVals = 1:10;
sprintf(‘There were %8.2f cows in the barnn’,xVals)
Change this so there are only integer values – what
does the “8.2” become?
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
Despite the way it looks, the computer
always considers a matrix as one big list,
working down one column at a time
This example was created on the
student edition of MATLAB – Notice that
the prompt is EDU
try this at your desk
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
sprintf with multiple
values
X=1:10
Y=5*X
mytable=[X;Y]
fprintf('5 times %3.0f is %3.0f n',mytable)
notice– the table is transposed from what you would
do for the disp() command
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
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 %
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
Try these
widgets=9;
sprintf('%d widgets', widgets)
fprintf('%d widgets', widgets)
printf('%d widgets', widgets)
sprintf('%d widgetsn', widgets)
fprintf('%d widgetsn', widgets)
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
Here’s another example that
uses the input, disp and
sprintf (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.
(degrees*pi/180 = radians)
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
Input
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
Graphical Input
(input from a plot)
• You can enter ordered pairs of x
and y values, by picking them off
a graph with ginput()
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
ginput problem
The book and Matlab help say
[x,y] = ginput
Retrieves a set of ordered pairs from the
graph everytime the enter key is struck
• But it didn't work on my computer
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
Always give the number
of values, n
ginput(n)
[x,y]=ginput(n)
Retrieves n ordered pairs
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
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
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
Try these in your Command
Window
plot(sin(linspace(0,4*pi,100)))
[xVals,yVals]=ginput(3)
[xVals,yVals]=ginput(1)
% find (approximately) the minimum
value
xVals=linspace(0,pi,300)
plot(xVals, exp(-sin(xVals))
[xMin,yMin]=ginput(1)
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
Summary - sprintf
• The sprintf function is more
versatile than the disp function
and allows you to combine text
and numeric information in
formatted output
(Table on page 238)
MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to:
Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
100 200 300 400 500
100
200
300
400
500
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
-5
-4.5
-4
-3.5
-3
-2.5
-2
-1.5
-1
-0.5
0
Rate of Change
time, hour
R
a
t
e
o
f
t
e
m
p
e
r
a
t
u
r
e
c
h
a
n
g
e
,
d
e
g
r
e
e
s
/
h
o
u
r
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

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Chapter 7 - User Controlled Input and Output

  • 1. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r User Controlled Input and Output Chapter 7
  • 2. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r In this chapter we’ll learn how to… • Prompt the user to enter data into an M-file program • Make your output look better • Use cell mode (debug feature)
  • 3. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r Section 7.2 Output Options • Enter the name of a variable in the Command Window • Use the disp() function • Use the sprintf() (or fprintf()) function
  • 4. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r disp The display (disp) function can be used to display the contents of a matrix without printing the matrix name
  • 5. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r 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.
  • 6. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r Since the disp function only takes one input, you must combine arrays to make more complicated output • One way to have a single-line output is to use the num2str(x) function to change numeric information to a string x=[35 2] disp(['The values in the x array are: ' num2str(x)])
  • 7. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r 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
  • 8. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r 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')
  • 9. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r This MATLAB program mimics a conversation, by using the input and disp functions. Watch the interactions as it runs in the next slide
  • 10. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r Formatted Output • sprintf 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
  • 11. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r sprintf vs fprintf sprintf is designed to generate a character string fprintf is designed to write to a file Syntax for both functions is very similar We will primarily use sprintf in MA 116
  • 12. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r sprintf vs fprintf We can use sprintf to generate a title title(sprintf('%d widgets', widgets)) (this won't work with fprintf) The book uses fprintf(), but they work nearly the same. sprintf('%d widgets', widgets) fprintf('%d widgets', widgets)
  • 13. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r sprintf Arguments sprintf('%d widgets', widgets) • format-string • includes place holders and formatting information for numbers • list of variables
  • 14. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r sprintf • The sprintf command is more flexible than the disp command, and allows you to put both variables and text onto the same line
  • 15. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r Place holder for your variable value Variable 8 total spaces 2 after the decimal point floating point format
  • 16. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r You can also use exponential format
  • 17. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r X is a matrix n is a carriage return
  • 18. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r sprintf xVals = 1:10; sprintf(‘There were %8.2f cows in the barnn’,xVals) Change this so there are only integer values – what does the “8.2” become?
  • 19. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r Despite the way it looks, the computer always considers a matrix as one big list, working down one column at a time This example was created on the student edition of MATLAB – Notice that the prompt is EDU try this at your desk
  • 20. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r sprintf with multiple values X=1:10 Y=5*X mytable=[X;Y] fprintf('5 times %3.0f is %3.0f n',mytable) notice– the table is transposed from what you would do for the disp() command
  • 21. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r 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 %
  • 22. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r Try these widgets=9; sprintf('%d widgets', widgets) fprintf('%d widgets', widgets) printf('%d widgets', widgets) sprintf('%d widgetsn', widgets) fprintf('%d widgetsn', widgets)
  • 23. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r Here’s another example that uses the input, disp and sprintf (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. (degrees*pi/180 = radians)
  • 24. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r
  • 25. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r Input
  • 26. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r Graphical Input (input from a plot) • You can enter ordered pairs of x and y values, by picking them off a graph with ginput()
  • 27. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r ginput problem The book and Matlab help say [x,y] = ginput Retrieves a set of ordered pairs from the graph everytime the enter key is struck • But it didn't work on my computer
  • 28. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r Always give the number of values, n ginput(n) [x,y]=ginput(n) Retrieves n ordered pairs
  • 29. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r 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
  • 30. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r Try these in your Command Window plot(sin(linspace(0,4*pi,100))) [xVals,yVals]=ginput(3) [xVals,yVals]=ginput(1) % find (approximately) the minimum value xVals=linspace(0,pi,300) plot(xVals, exp(-sin(xVals)) [xMin,yMin]=ginput(1)
  • 31. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r Summary - sprintf • The sprintf function is more versatile than the disp function and allows you to combine text and numeric information in formatted output (Table on page 238)
  • 32. MATLAB for Engineers, by Holly Moore. ISBN 0-13-187244-3. © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458. 100 200 300 400 500 100 200 300 400 500 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 Rate of Change time, hour R a t e o f t e m p e r a t u r e c h a n g e , d e g r e e s / h o u r 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