Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
-5
0.5
1.5
2.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Objectives
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
Rate of Change
-5
0.5
1.5
2.5
time, hour
100
200
300
To this point we have hard coded the values of variables into our M-file programs The input function allows us to prompt the user to enter a value
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
The input function is used in an M-file program to prompt the user to enter a value
100 200
300
400
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
time, hour
Scalars Matrices
enter inside square brackets
100
200
Character strings
100 200 300 400 500
300
400
500
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
Run this program twice more once with numeric input and once with character input
Matrix input
100 200
300
400
Character input
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
time, hour
100
200
Enter the name of a variable Use the disp function Use the fprintf function Use the sprintf function
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
disp
3.5 4 4.5 5
0.5
1.5
2.5
time, hour
100
200
300
400
The display (disp) function can be used to display the contents of a matrix without printing the matrix name
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
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 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
Since the disp function only takes one input, you must combine arrays to make more complicated output
3.5 4 4.5 5
0.5
1.5
2.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
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
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
Hint
3.5 4 4.5 5
0.5
1.5
2.5
time, hour
100
200
300
400
If you want to include an apostrophe in a string, you need to enter the apostrophe twice. If you dont, MATLAB thinks the apostrophe terminates the string. For example: disp('The moon''s gravity is 1/6th that of the earth')
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
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 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
Formatted Output
3.5 4 4.5 5
0.5
1.5
2.5
time, hour
100
200
300
400
fprintf 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 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
fprintf Arguments
3.5 4 4.5 5
0.5
1.5
2.5
time, hour
format-string
includes place holders and formating information for numbers
100
list of matrices
100 200 300 400 500
200
300
400
500
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
fprintf
3.5 4 4.5 5
0.5
1.5
2.5
time, hour
100
200
300
The fprintf command is more flexible than the disp command, and allows you to put both variables and text onto the same line
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
Specifier
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
Description
Single character
Decimal notation (signed) Exponential notation
time, hour
%c
%d %e
%E
100 200
Exponential notation
Fixed-point notation The more compact of %e and %f. Insignificant zeros do not print String of characters Decimal notation unsigned
%f %g
100 200 300 400 500
300
400
500
%s %u
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
8 total spaces Place holder for 2 after the decimal point your variable value floating point format
100 200 300 400 500
Variable
300
400
500
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
500
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
This example was created on the student edition of MATLAB Notice that the prompt is EDU
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Despite the way it looks, the computer always considers a matrix as one big list, working down one column at a time
Rate of Change
-5
Hint
3.5 4 4.5 5
0.5
1.5
2.5
time, hour
100
200
300
400
One of the most common mistakes new programmers make is to forget to include the f in the placeholder sequence. The fprintf function wont work, but no error message is returned either.
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
Hint
3.5 4 4.5 5
0.5
1.5
2.5
time, hour
If you want to include a percentage sign in an fprintf statement, you need to enter the % twice. If you dont, MATLAB thinks the % is a placeholder for data. For example:
fprintf('The interest rate is %5.2f %% \n', 5)
100
200
300
400
Rate of Change
-5
Heres another example that uses the input, disp and fprintf functions
3.5 4 4.5 5
0.5
1.5
2.5
time, hour
100
200
300
400
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.
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
Input
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
time, hour
100
200
300
First create and open an empty output file, and assign it a file identifier Add the file identifier to the first field in the fprintf function
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
time, hour
r r+ w
Open an existing file for reading only Open an existing file for reading and writing Delete the contents of an existing file (or create a new file) and open it for writing only. Delete the contents of an existing file (or create a new file) and open it for reading and writing Open an existing file (or create a new file) and open it for writing only, appending to the end of file. Open an existing file (or create a new file) and open it for reading and writing, appending to the end of file.
100
200
300
w+
100 200 300 400 500
400
500
a a+
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
The result in the command window is the number of characters in the output string
300
400
The output string is sent to the specified file Add the file identifier to the first field in the fprintf function
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Double clicking on the file name in the current directory window causes an editing window to open which displays the contents of the file
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
400
500
fclose(fid)
0.00 0.10 0.20 0.30 0.40 0.50 0.60 0.70 0.80 0.90 1.00
1.00000000 1.10517092 1.22140276 1.34985881 1.49182470 1.64872127 1.82211880 2.01375271 2.22554093 2.45960311 2.71828183
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
sprintf
3.5 4 4.5 5
0.5
1.5
2.5
time, hour
100
200
300
400
The sprintf function is similar to fprintf Instead of just sending the output to the command window, sprintf assigns the output a name, and then sends it to the command window
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
500
Why is this useful? sprintf could be used to specify a string that is then used in a graph annotation
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
time, hour
100
You can enter ordered pairs of x and y values, by picking them off a graph ginput
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
ginput
3.5 4 4.5 5
0.5
1.5
2.5
time, hour
[x,y] = ginput
Retrieves a set of ordered pairs from the graph everytime the enter key is struck
100
200
300
[x,y]=ginput(n)
100 200 300 400 500
400
500
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
Floating Crosshair
100 200 300 400 500
400
500
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 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
Publish Icon
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time, hour
100
200
300
400
M-files that use cell mode, such as this one used to solve homework problems from chapter 5, can be published using MATLABs publish feature
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
To publish in a different format, such as Word or PowerPoint, change the publish configuration
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
time, hour
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Section 7.5
Rate of Change
-5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0
0.5
1.5
2.5
3.5
4.5
time, hour
Reading and Writing Data from Files Some common types of data files are
dat txt xls jpg
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
Import Wizard
3.5 4 4.5 5
0.5
1.5
2.5
time, hour
100
200
300
400
Use the import wizard to determine the data type and to suggest ways to represent the data Launch from the file menu From the uiimport funtion
uiimport(filename.extension)
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
time, hour
To import ASCII data, you must know how the data in the file is formatted. For example, many ASCII data files use a fixed (or uniform) format of rows and columns.
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
(continued )
3-84
0.5
1.5
2.5
3.5
4.5
time, hour
Are the data items numeric, text strings, or a mixture of both types?
100 200
300
400
500
What character is used as the delimiter, that is, the character used to separate the data items in each row? The delimiter is also called the column separator.
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
3-85
(continued )
0.5
1.5
2.5
3.5
4.5
time, hour
You can use the Import Wizard to import many types of ASCII data formats, including data on the clipboard. When you use the Import Wizard to create a variable in the MATLAB workspace, it overwrites any existing variable in the workspace with the same name without issuing a warning. The Import Wizard presents a series of dialog boxes in which you:
100
200
300
400
1. 2. 3.
3-86
Specify the name of the file you want to import, Specify the delimiter used in the file, and Select the variables that you want to import.
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
time, hour
100
200
300
400
The import wizard requires interaction with the user MATLAB includes a series of import functions that automatically import the data You need to know what the data type is before you can use these
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
Import functions
3.5 4 4.5 5
0.5
1.5
2.5
time, hour
To import a .mat or .dat file, use the load command Other functions can be found if you type
doc fileformats
100
200
300
400
For example, wavread can be used for wav files xlsread can be used for Excel files
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
1234 5 6 7 8 9 10
1; 2; 3; 4; 5 6; 7; 8; 9; 10 or 1, 2, 3, 4, 5 6, 7, 8, 9, 10 Ann Type1 12.34 45 Yes Joe Type2 45.67 67 No
100 200 300 400 500
100
200
300
400
500
Grade1 Grade2 Grade3 91.5 89.2 77.3 88.0 67.8 91.0 67.3 78.1 92.5
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
Exporting Data
3.5 4 4.5 5
0.5
1.5
2.5
time, hour
100
200
300
400
Use the save function for .mat or .dat files Use specialized functions for other file types For example
xlswrite for Excel files
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
Use the file menu to save a file interactively, using a variety of file formats
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
time, hour
100
200
300
400
The moth found trapped between a relay in Harvards MarkII Aiken Relay Computer Image courtesy of the Naval Surface Warfare Center
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Rate of Change
-5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
0.5
1.5
2.5
3.5
4.5
time, hour
100
200
300
400
500
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.
Step in
Step out
Rate of Change
-5
0.5
1.5
2.5
3.5
4.5
time, hour
Arrange documents
100
200
300
400
MATLAB for Engineers 3E, by Holly Moore. 2011 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material 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.