0% found this document useful (0 votes)
4 views

SKMM1013-Lecture15_matlab-functions

Uploaded by

andcloud54
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SKMM1013-Lecture15_matlab-functions

Uploaded by

andcloud54
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lecture 15

MATLAB: Functions & Files

Dr M Kasim A Jalil
Faculty of Mechanical Engineering
UTM
Built--in Functions
Built

ƒ exp(x),
( ) sqrt(x),
( ) llog(x),
( ) llog10(x),
10( ) cos(x),sin(x),acos(x)….
( ) i ( ) ( )
ƒ Refer to table 3.1-1 & 3.1-2 pg 142 & 146

© Dr. MKAJ, FKM, UTM


User--Defined Functions
User

ƒ SSyntax
function [output variables] = function_name (input variables)
definition of variables
ƒ Example
function z = fun (x,y) >>w=fun(3,7)

u= 3*x;
3* w=
303
z= u + 6*y.^2

© Dr. MKAJ, FKM, UTM


Anonymous Functions
ƒ Creating
C ti a ffunction
ti att th
the commandd promptt without
ith t needing
di
to create an M-file
ƒ Syntax
function_handle = @(arglist) expr
where - arglist is a comma-separated list of input arguments
- expr is any single valid MATLAB expression
ƒ Example
>> sqrtsum
q = @((x,y)
,y) sqrt(x.^2
q ( + yy.^2);); >>sqrtsum (3, 4)
ans =
5

© Dr. MKAJ, FKM, UTM


Opening a File
ƒ SSyntax
t
FID=fopen (‘filename.ext’,’status’) – status is ‘r’, ‘w’, ‘r+w’,…
fprintf (FID, ‘variable format’, variables..)
fclose (FID)
ƒ Example
myfile=fopen
myfile fopen ((‘student
student.dat
dat’,’r’)
r)
fprintf (myfile, ‘%f %f %f’, test1, test2, test3)
fclose (myfile)
ƒ Explore other command – fscanf, fread, etc..

© Dr. MKAJ, FKM, UTM

You might also like