Chapter 7 - User Controlled Input and Output
-
Upload
siva-gopal -
Category
Documents
-
view
71 -
download
4
Transcript of Chapter 7 - User Controlled Input and Output
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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
Section 7.2Output 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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
Place holder for your variable value
Variable8 total spaces2 after the decimal pointfloating 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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
sprintf
xVals = 1:10;sprintf(‘There were %8.2f cows in the barn\n’,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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
sprintf with multiple values
X=1:10Y=5*Xmytable=[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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
Try these
widgets=9;sprintf('%d widgets', widgets)fprintf('%d widgets', widgets)printf('%d widgets', widgets)sprintf('%d widgets\n', widgets)fprintf('%d widgets\n', 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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour Always give the number
of values, nginput(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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
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
Rate of temperature change, degrees/hour
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