To days Outline

20
MATLAB and Simul ink lecture 3 1 To days Outline Functions Strings Sparse Arrays Cell Arrays Structures Exercises on this days topics

description

To days Outline. Functions Strings Sparse Arrays Cell Arrays Structures Exercises on this days topics. Functions files. Runs in its own independent workspace Receives input data through an Input argument list Returns data through an Output argument list. Functions files. - PowerPoint PPT Presentation

Transcript of To days Outline

Page 1: To days Outline

MATLAB and Simulink

lecture 3 1

To days Outline Functions Strings Sparse Arrays Cell Arrays Structures Exercises on this days topics

Page 2: To days Outline

MATLAB and Simulink

lecture 3 2

Functions files

Runs in its own independent workspace

Receives input data through an Input argument list

Returns data through an Output argument list

Page 3: To days Outline

MATLAB and Simulink

lecture 3 3

Functions files The first word written in the file must be

the word function function [outarg1,…] = fname(inarg1,…)

%Comment lines…(Executable code)…(return)

Page 4: To days Outline

MATLAB and Simulink

lecture 3 4

Functions files

Variable passing When a function calls occurs.

MATLAB makes a copy of the arguments

If the function modifies the input argument it will not affect the original data in the caller.

This apply both scalars and arrays.

Page 5: To days Outline

MATLAB and Simulink

lecture 3 5

Example

Create a function that adds a value to the input argument and returns the new value.

Page 6: To days Outline

MATLAB and Simulink

lecture 3 6

Example

In command window>> A=[1 2];>> B=myfun(A);Input argument before modifying: [1 2]Input argument after modifying: [11 12]>> disp(A) 1 2>> disp(B) 11 12

Page 7: To days Outline

MATLAB and Simulink

lecture 3 7

Functions files Optional Arguments

Many function supports optional input and output arguments

We have used the plot command with several different Input argument lists.

The max command accepts different output arguments

How do MATLAB functions know how many arguments are present and how do they adjust their behavior?

Page 8: To days Outline

MATLAB and Simulink

lecture 3 8

Functions files MATLAB offers some special functions to get

information about the arguments and to report errors nargin Returns the actual number of input

arguments used to call the function. nargout Returns the actual number of output

arguments used to call the function. error(msg) Display error message and abort

the function producing the error. warning(msg) Display warning message

Execution can continue nargchk Returns a standard error message if a

function is called with to few or to many arguments

Page 9: To days Outline

MATLAB and Simulink

lecture 3 9

ExampleThe location of a point in the Cartesian plane can be represented either in rectangular (x,y) or polar coordinates (r,θ).

)cos(rx 22 yxr

)sin(ry xy1tan

• Write a function rect2polar that converts rectangular coordinates to polar coordinates.

• The function is design to support two input arguments (x,y)• However if only one input argument is supplied the functions

should suppose that y is equal to zero.• The function normally returns both magnitude and angle but if

only one output argument is present, it returns only the magnitude

Page 10: To days Outline

Test the function: In command window:>> [mag,angle]=rect2polar??? Error using ==> rect2polarNot enough input arguments.>> [mag,angle]=rect2polar(1,-1,2)??? Error using ==> rect2polarToo many input arguments.>> [mag,angle]=rect2polar(1)mag =1angle = 0>> [mag,angle]=rect2polar(1,1)mag = 1.4142angle = 45>> [mag]=rect2polar(1,1)mag = 1.4142

Page 11: To days Outline

MATLAB and Simulink

lecture 3 11

Functions files

Global Memory Provides a way to share data between

functions A global memory is declared with the

global statement. global var1 var2 …

Page 12: To days Outline

MATLAB and Simulink

lecture 3 12

Functions files

Preserving data When a function finishing executing the

special workspace will be destroyed. Persistent memory

Only accessed within the function A persistent variable is declared using

the persistent statement persistent var1 var2 …

Page 13: To days Outline

MATLAB and Simulink

lecture 3 13

Functions files

Subfunctions or Internal functions

More than one function in a single file

Top function is the normal function The ones below are subfunctions

or Internal functions

Page 14: To days Outline

MATLAB and Simulink

lecture 3 14

Strings

In MATLAB a string is an array of type char. str=’This Is a string’

Creating two-dimensional Character arrays Each row must have the same length

MATLAB offers a number of string operations strcmp(str1,str2) Compares two strings

Returns 1 if true. See MATLAB help for more string operations

Page 15: To days Outline

MATLAB and Simulink

lecture 3 15

Sparse Arrays

Sparse arrays are special arrays in which memory is only allocated for the nonzero elements.

using sparse arrays saves memory and can make a program run faster. sparse(A) Converts A to a sparse

array

Page 16: To days Outline

MATLAB and Simulink

lecture 3 16

Cell Arrays

A cell array is a special array that can contain different data: Strings Complex numbers vectors

Cell array uses braces {} instead of parentheses () for selecting cells

To view a cell array graphically use cellplot(A)

Page 17: To days Outline

MATLAB and Simulink

lecture 3 17

Structures Structures can be created in two ways

1. A field at a time using assignment statements.

>>student.name='Carl';>>student.addr='Borlänge';>>student.phone=778858;student = name: 'Carl' addr: 'Borlänge' phone: 778858

Page 18: To days Outline

MATLAB and Simulink

lecture 3 18

Structures

A second student can be added to the structure by using a subscript

>>student(2).name=’John’; The other field will automatically

be initialized as an empty array. Accessing field in the structure

using parentheses

Page 19: To days Outline

MATLAB and Simulink

lecture 3 19

Structures

2. All at once using the struct function str_array=struct(field1,val1,field2,val2) Example:

student=struct('Name',{'Carl','John'},'Addr',...

'Borlänge','Falun'},'Phone',{778858,123456})

This will construct a 1x2 structure.

Page 20: To days Outline

MATLAB and Simulink

lecture 3 20

Exercises on this days topics 5.1 , 5.2, 5.10(8) , 5.16(13) ,

5.19(16), 5.24(20) 6.1 , 6.20(18) 7.6

The exercise numbers are referring to the book MATLAB Programming for engineers, third edition 20(second edition).