Вы находитесь на странице: 1из 19

Introduction to MATLAB

Programming

Introduction to MATLAB

Programming - 2

Programs are contained in m-files Plain files not binary files produced by word Files must have an .m extension m-files must be in path When you call an M-file function from either the command line or from within another M-file, MATLAB parses the function and stores it in memory. This prevents MATLAB from having to reparse a function each time you call it during a session. The compiled function remains in memory until you clear it using the CLEAR command, or until you quit MATLAB.

Introduction to MATLAB TWO TYPES OF M-FILES


1. Scripts - Automate long sequences of commands

Programming - 3

2. Functions - provide extensibility to MATLAB. They allow you to add new functions to the existing functions

Introduction to MATLAB
Script M-files

Programming - 4

Standard ASCII text files Contains series of MATLAB expressions stored together in a predefined sequence. Saved with an .m extension and are called by simply typing the filename without the extension in the command window.

Downside of using Script files: - All variables created are added to the workspace. - Variables already existing in the workspace may be overwritten

Introduction to MATLAB
Functions M-files

Programming - 5

Functions are subprograms Use input and output parameters to communicate with other function

Differences between Script & Function M-files: Structural Syntax - must contain keyword FUNCTION at the beginning of the first line

Function Workspaces, Inputs & Outputs A function does not work with variables in the base MATLAB workspace. As a result, all information to be transferred between the MATLAB workspace and a function must be passed as inputs & outputs.

Introduction to MATLAB
Structure of a Function M-file
Keyword: function

Programming - 6

Function Name (same as file name .m)

Output Argument(s)
function y = mean(x)

Input Argument(s)

% MEAN Average or mean value.


Online Help % For vectors, MEAN(x) returns the mean value. % For matrices, MEAN(x) is a row vector % containing the mean value of each column. [m,n] = size(x); MATLAB Code

if m == 1
m = n;

end
y = sum(x)/m; output_value = mean(input_value)

Command Line Syntax

Introduction to MATLAB
Text Input and Output
Inputs to Functions :

Programming - 7

input function can be used to prompt the user for numeric or string input Input parameters to functions are preferred

Text output from functions: disp function for simple output fprintf function for formatted output.

Introduction to MATLAB
Prompting for User Input

Programming - 8

The input function can be used to prompt the user for numeric or string input. Syntax: entry = input (Text Display)

entry = input (Text Display, s )


Examples: >> x = input (Enter a value for x) Enter a value for x
%serves as a prompt on the screen that signifies that MATLAB is awaiting for user response. %like the previous command but will return the entered string as a text variable rather than as a variable name or numerical value.

>>yourName = input (Enter your name,s);

Introduction to MATLAB
Text Output with disp - Output to the command window is achieved with the disp

Programming - 9

function disp Simple to use. Provides limited control over appearance of output. Syntax: disp (variable) disp (text string) Examples: >>poly_roots = [ 1.2400 2.5600 5.6400 ] >> disp ('The roots of the cubic polynomial are') The roots of the cubic polynomial are >> disp (poly_roots) 1.2400 2.5600 5.6400 >> disp ('The roots of the cubic polynomial are') , disp (poly_roots) The roots of the cubic polynomial are 1.2400 2.5600 5.6400

Introduction to MATLAB
disp

Programming - 10

Simple to use. Provides limited control over appearance of output.

Syntax: disp (variable) disp (text string)


Examples: >>poly_roots = [ 1.2400 2.5600 5.6400 ] >> disp ('The roots of the cubic polynomial are') The roots of the cubic polynomial are >> disp (poly_roots) 1.2400 2.5600 5.6400 >> disp ('The roots of the cubic polynomial are') , disp (poly_roots) The roots of the cubic polynomial are 1.2400 2.5600 5.6400

Introduction to MATLAB

Programming - 11

Relational and Logical Operators


Operators < > <= >= ~= == & | ~ xor Description Less than Greater than Less than or equal to Greater than or equal to Not equal to Equal to AND OR NOT Exclusive OR

-Will output 1 for true conditions and 0 for false conditions - Can be used with scalars and arrays

Introduction to MATLAB

Programming - 12

Other Logical built-in functions


Operators
all (A)

Description
Returns true if all elements in a vector A are true. Returns false if one or more elements are false Returns 1 (true) if any element in a vector A is true (nonzero). Returns 0 (false) if all elements are false. If A is a vector, returns the indices of the nonzero elements If A is a vector, returns the address of the elements that are larger than d (any relational operator can be used)

any (A) find (A)

find (A>d)

Introduction to MATLAB
SUMMARY

Programming - 13

Relational operators involve comparison of two values. The result of a relational operation is a logical (True/False)

value.
Logical operators combine (or negate) logical values to

produce another logical value.

Introduction to MATLAB

Programming - 14

Example : Analysis of temp data The following were the daily max temp in (F) in Washington during the month of April,2002. 58 73 73 53 30 48 56 73 73 66 69 63 74 82 84 91 93 89 91 80 59 69 56 64 63 66 64 74 63 69.

Use relational logical operators to determine the following: a. The number of days the temp was above 75 b. the number of days the temp was between 65 and 80

Introduction to MATLAB
Examples:
>> r = [ 8 12 9 4 23 19 10 ] r= 8 12 9 4 23 19 10 >>s=r<=10

Programming - 15

%checks which r elements are smaller or equal to 10 1 1 0 0 1

s=
1 t= 0

>>t=r(s) 8
w= 8 9 4

% use s for addresses in vector r to create a vector t 4 10


% previous operations can be done in one step 10

>> w=r(r<=10)

Introduction to MATLAB
fprint & sprintf

Programming - 16

Introduction to MATLAB Example

Programming - 17

function areacircle r=input('Give radius of circle: '); a=pi*r^2; fprintf('The area of circle with radius %.2f is %.6f\n',r,a); end

Introduction to MATLAB

Programming - 18

Flow control - selection

The if-elseif-else construction


if <logical expression> <commands> elseif <logical expression> <commands> else <commands> end

if height>170 disp(tall) elseif height<150 disp(small) else disp(average) end

Introduction to MATLAB
Example:

Programming - 19

The following script file will returns one of the following messages: teenager, adult and senior given as the input of the variable age. function check_age age=input('Enter your age: ') if age<=18 disp('teenager') elseif age<=52 disp('adult') else disp('senior') end

Вам также может понравиться