chapter 7 - user controlled input and output
TRANSCRIPT
![Page 1: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/1.jpg)
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
![Page 2: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/2.jpg)
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)
![Page 3: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/3.jpg)
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
![Page 4: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/4.jpg)
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
![Page 5: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/5.jpg)
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.
![Page 6: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/6.jpg)
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)])
![Page 7: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/7.jpg)
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
![Page 8: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/8.jpg)
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')
![Page 9: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/9.jpg)
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
![Page 10: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/10.jpg)
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
![Page 11: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/11.jpg)
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
![Page 12: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/12.jpg)
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)
![Page 13: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/13.jpg)
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
![Page 14: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/14.jpg)
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
![Page 15: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/15.jpg)
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
![Page 16: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/16.jpg)
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
![Page 17: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/17.jpg)
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
![Page 18: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/18.jpg)
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?
![Page 19: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/19.jpg)
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
![Page 20: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/20.jpg)
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
![Page 21: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/21.jpg)
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 %
![Page 22: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/22.jpg)
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)
![Page 23: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/23.jpg)
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)
![Page 24: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/24.jpg)
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
![Page 25: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/25.jpg)
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
![Page 26: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/26.jpg)
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()
![Page 27: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/27.jpg)
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
![Page 28: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/28.jpg)
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
![Page 29: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/29.jpg)
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
![Page 30: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/30.jpg)
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)
![Page 31: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/31.jpg)
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)
![Page 32: Chapter 7 - User Controlled Input and Output](https://reader034.vdocuments.us/reader034/viewer/2022042513/58712e6f1a28abe4448b7c07/html5/thumbnails/32.jpg)
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