Sei sulla pagina 1di 71

Rate of Change

-5

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

User-Controlled Input and Output


Chapter 7
3.5 4 4.5 5

0.5

1.5

2.5

time, hour

100

200

300

400

500 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.

Objectives
Rate of Change
-5

Rate of temperature change, degrees/hour

-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

After studying this chapter you should be able to:


Prompt the user for input to an M-file program Create output using the disp function Create formatted output fprintf sprintf Use graphical techniques to provide program input Use cell mode to modify and run M-file programs
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.

100

200

300

400

500 100 200 300 400 500

Rate of Change
-5

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

Section 7.1 User Defined Input


3.5 4 4.5 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

500 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

Rate of temperature change, degrees/hour

-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

The input function is used in an M-file program to prompt the user to enter a value
100 200

300

400

500 100 200 300 400 500

The prompt is displayed in 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

Rate of temperature change, degrees/hour

-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

500 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

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

Input accepts a variety of data


3.5 4 4.5 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

enter inside single quotes Or specify string input with s

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

Rate of temperature change, degrees/hour

-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

Run this program twice more once with numeric input and once with character input

Matrix input
100 200

300

400

500 100 200 300 400 500

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

Rate of temperature change, degrees/hour

-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

Indicates that the input should be interpreted as a string

100

200

300

400

500 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

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

Section 7.2 Output Options


3.5 4 4.5 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

500 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

Rate of temperature change, degrees/hour

disp
3.5 4 4.5 5

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

0.5

1.5

2.5

time, hour

100

200

300

400

500 100 200 300 400 500

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

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

The disp function can also be used to display a string


3.5 4 4.5 5

0.5

1.5

2.5

time, hour

100

200

300

400

500 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

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

Strings are really arrays of character information


3.5 4 4.5 5

0.5

1.5

2.5

time, hour

100

200

300

400

500 100 200 300 400 500

The result is a character array

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

Rate of temperature change, degrees/hour

-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

500 100 200 300 400 500

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

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

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

Use the num2str(x) function to change numeric information to a string


disp(['The values in the x array are: ' num2str(x)])

100

200

300

400

500 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

Rate of temperature change, degrees/hour

-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

500 100 200 300 400 500

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

Rate of temperature change, degrees/hour

Hint
3.5 4 4.5 5

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

0.5

1.5

2.5

time, hour

100

200

300

400

500 100 200 300 400 500

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

Rate of temperature change, degrees/hour

-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

500 100 200 300 400 500

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

Rate of temperature change, degrees/hour

-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

500 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

Rate of temperature change, degrees/hour

Formatted Output
3.5 4 4.5 5

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

0.5

1.5

2.5

time, hour

100

200

300

400

500 100 200 300 400 500

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

Rate of temperature change, degrees/hour

fprintf Arguments
3.5 4 4.5 5

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

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

Rate of temperature change, degrees/hour

fprintf
3.5 4 4.5 5

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

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

500 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

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

Common format specifier notation for fprintf

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

Rate of temperature change, degrees/hour

-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

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

Rate of temperature change, degrees/hour

-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

You can also use exponential format


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

Rate of temperature change, degrees/hour

-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

X is a matrix /n is a carriage return

100

200

300

400

500 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

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

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

500 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.

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

Rate of temperature change, degrees/hour

Hint
3.5 4 4.5 5

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

0.5

1.5

2.5

time, hour

100

200

300

400

500 100 200 300 400 500

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

Rate of temperature change, degrees/hour

Hint
3.5 4 4.5 5

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

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

500 100 200 300 400 500

results in: The interest rate is 5.00 %


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

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

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

500 100 200 300 400 500

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

Rate of temperature change, degrees/hour

-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

500 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

Rate of temperature change, degrees/hour

-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

Input

time, hour

100

200

300

400

500 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

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

Sending formatted output to a file


3.5 4 4.5 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

500 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

Rate of temperature change, degrees/hour

-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

File identifier File name any legitimate MATLAB name will do


100 200

flag that identifies this as a file to which we can write

300

400

500 100 200 300 400 500

Create and open an output file and assign it a file identifier

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

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

Importing - Exporting data

fopen file permissions


0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 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

Rate of temperature change, degrees/hour

-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

The result in the command window is the number of characters in the output string

300

400

500 100 200 300 400 500

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

Rate of temperature change, degrees/hour

-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

500 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.

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

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

Exporting data Functions fopen, fprintf and fclose

0.5

1.5

2.5

3.5

4.5

time, hour

Exp.txt x = 0:0.1:1; Y = [x; exp(x)]; fid = fopen('Exp.txt','w');


100 200 300

fprintf(fid,'%6.2f %12.8f \n',Y);


100 200 300 400 500

File created in current directory

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

Rate of temperature change, degrees/hour

sprintf
3.5 4 4.5 5

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

0.5

1.5

2.5

time, hour

100

200

300

400

500 100 200 300 400 500

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

Rate of temperature change, degrees/hour

-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

The result from fprintf is a character count

200

300

The result from sprintf is the actual string


100 200 300 400 500

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

Rate of temperature change, degrees/hour

-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

500 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

Rate of temperature change, degrees/hour

-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

500 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

Rate of temperature change, degrees/hour

-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

500 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

Rate of temperature change, degrees/hour

-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

500 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

Rate of temperature change, degrees/hour

7.3 Graphical Input


3.5 4 4.5 5

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

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

500 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

Rate of temperature change, degrees/hour

ginput
3.5 4 4.5 5

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

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

Retrieves n ordered pairs

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

Rate of temperature change, degrees/hour

-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

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

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

Section 7.4 More Cell Mode Features


3.5 4 4.5 5

0.5

1.5

2.5

time, hour

Publish Value Manipulation

100

200

300

400

500 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

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

Publish Icon
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

time, hour

100

200

300

400

500 100 200 300 400 500

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

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

Results published in HTML


0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

time, hour

100

200

300

400

500 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.

To publish in a different format, such as Word or PowerPoint, change the publish configuration
Rate of Change
-5

Rate of temperature change, degrees/hour

-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

500 100 200 300 400 500

From the file menu select

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.

FilePublish Configuration for

Rate of Change
-5

Rate of temperature change, degrees/hour

Value Manipulation Tools


3.5 4 4.5 5

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

0.5

1.5

2.5

time, hour

Increment and and Divide decrement value multiply value


100

200

300

400

500 100 200 300 400 500

This is the number that is changed, because of the cursor placement

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

Rate of temperature change, degrees/hour

100

200

300

400

500 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

Rate of temperature change, degrees/hour

Import Wizard
3.5 4 4.5 5

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

0.5

1.5

2.5

time, hour

100

200

300

400

500 100 200 300 400 500

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

Rate of temperature change, degrees/hour

-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

500 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

Rate of temperature change, degrees/hour

-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

500 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

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

The Import Wizard


0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 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

500 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.

(continued )

3-84

The Import Wizard (continued)


Rate of Change
-5

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

For these files, you should know the following.


How many data items are in each row?

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

Does each row or column have a descriptive text header?


100 200 300 400 500

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 )

The Import Wizard (continued)


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

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:

Rate of temperature change, degrees/hour

100

200

300

400

500 100 200 300 400 500

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

Rate of temperature change, degrees/hour

-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

500 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

Rate of temperature change, degrees/hour

-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

500 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

Rate of temperature change, degrees/hour

-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

500 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

Command line approaches


3.5 4 4.5 5

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

0.5

1.5

2.5

time, hour

100

200

300

400

500 100 200 300 400 500

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

Rate of temperature change, degrees/hour

Import functions
3.5 4 4.5 5

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

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

500 100 200 300 400 500

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

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

Importing data - Summary


Data Format Sample File Extension Matlab function

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

.txt .dat or other


.txt .dat .csv or other .txt .dat or other

load dlmread or csvread textread or fscanf textread or fscanf

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

.txt .dat or other

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

Rate of temperature change, degrees/hour

Exporting Data
3.5 4 4.5 5

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

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

500 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

Rate of temperature change, degrees/hour

-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

Use the file menu to save a file interactively, using a variety of file formats

100

200

300

400

500 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

Rate of temperature change, degrees/hour

7.6 Debugging Your Code


3.5 4 4.5 5

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

0.5

1.5

2.5

time, hour

100

200

300

400

500 100 200 300 400 500

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

Rate of temperature change, degrees/hour

-4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0

The Error Bar

The Error Bar

0.5

1.5

2.5

3.5

4.5

time, hour

100

200

300

400

500 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 -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

M-file with an error on line 22

Rate of temperature change, degrees/hour

100

200

300

400

500 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.

Set/clear breakpoint icon


Rate of Change
-5

Continue to next breakpoint

Rate of temperature change, degrees/hour

-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

Step icon Breakpoint

100

200

300

400

The execution is paused here


100 200 300 400 500

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

Rate of temperature change, degrees/hour

-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

Arrange documents

100

200

300

400

500 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.

Potrebbero piacerti anche