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