Matlab_Session_4 - Matlab Tutorial Session 4 This tutorial...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Matlab Tutorial Session 4 This tutorial session covers the following topics 1. Variable number of arguments in functions 2. Return , break and continue commands 3. Advanced input and output including file input and output 4. find and findstr commands 5. Example programs: Output for Tecplot from Matlab, Reading a Tecplot datafile in Matlab Next tutorial will cover (i) cell arrays and structures, (ii) differential equation solvers, and (iii) introduction to symbolic math toolbox. Review of Session 3 Functions in Matlab are written in a separate M-file. This M-file begins with the header function output = functionname ( input1 , input2 , …, inputN ) The output may be a single variable (such as function T = trace ( M ) ), or there may be multiple output variables specified as a vector (such as function [r, theta] = polar ( cpx ) ). The input and output variables are not pre-typed to be integers, strings etc. The function is written assuming proper types of variables and it is up to the user (or calling program) to provide the function with proper arguments and collect the outputs. Otherwise the user will experience error in execution. A function has no access to any variables outside it. Everything required must be passed as arguments. There is an exception to this, which is to specifically define certain variables in the workspace as global using the global command. This is discussed in this session. Function M-file must be in the current directory or in matlab path so that matlab can find it. Otherwise matlab issues an error that such a function does not exist. Subfunction may be placed in a function M-file at the end. But they cannot access the main function’s variables. They must again be passed everything as input arguments. A function may be executed by the following syntax in the program: [out1, out2 , …, outM] = functionname ( inp1, inp2, …, inpN) or by using the feval command [out1, out2 , …, outM] = feval ( ‘functionname’ ,inp1,inp2,…,inpN)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The inline command can be used to define simple mathematical functions spontaneously, e.g. tryfunc = inline ( ‘sqrt(x*x + y*y + z*z’ ) User input can be obtained through input command. Output can be displayed more neatly by using disp command in conjunction with string functions such as strcat , num2str , mat2str . Two useful matrix-related commands are size (to obtain the size of a matrix in terms on number of rows and columns) and length (to obtain length of a vector). Empty matrix [] , Infinity inf , and Not a Number nan are special constants. They can be tested by isempty , isinf and isnan Matlab functions respectively. Passing variable number of arguments to functions There are two ways of receiving variable number of inputs in a function – When the maximum number of arguments is fixed and reasonable small : The function is defined in usual form with all arguments and Matlab accepts the user’s passing less number of arguments than those defined. However if user tries
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/11/2010 for the course CS 301 taught by Professor Nice during the Spring '10 term at A.T. Still University.

Page1 / 20

Matlab_Session_4 - Matlab Tutorial Session 4 This tutorial...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online