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

Functions and Files

This document discusses functions and files in MATLAB. It introduces various built-in mathematical functions like ceil, fix, floor, round, sign, sec, csc, and asec. It then explains how to create user-defined functions with input and output variables, and how these functions are saved in M-files with the same name as the function. The document also covers global variables, subfunctions, anonymous functions, and importing and exporting data to and from files.

Uploaded by

87vr772j64
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Functions and Files

This document discusses functions and files in MATLAB. It introduces various built-in mathematical functions like ceil, fix, floor, round, sign, sec, csc, and asec. It then explains how to create user-defined functions with input and output variables, and how these functions are saved in M-files with the same name as the function. The document also covers global variables, subfunctions, anonymous functions, and importing and exporting data to and from files.

Uploaded by

87vr772j64
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

FUNCTIONS AND FILES

by
Mohamed Hussein
Lecture 4

Introduction to Matlab
 Functions and Files
More Mathematical Functions
ceil (x) - round to the nearest integer
toward 
fix (x) - round to the nearest integer
toward 0 (zero)
floor (x) - round to the nearest integer
toward -
round (x) - round toward the nearest integer
sign (x) - +1 (if x>0), 0 (if x=0) or -1 (if x<0)
sec (x) - secant x
csc (x) - cosecant x
asec (x) - arc-secant x
User-Defined Functions
User own developed function is created using M-file with the
following features:
A function file must begins with function definition line with list of
inputs and outputs. This distinguishes a function M-file from script
M-file.
function [output variables]=function_name(input variables)
* the square bracket is optional if only one output variable is used

All variables in a function file are local which means their values
only available within the function (not in Matlab Command
window)
The filename the function being saved should have the same name
as the function (example: for function calculate the filename should
be calculate.m)
Use the command exist to check the availability of a filename
User-Defined Functions
Create the following function file (M-file) and save as cylinder1.m
function [cyl_area,cyl_volume]=cylinder1(radius,height)
%function to calculate area and volume of a cylinder
cyl_area= (2.*pi.*radius) .* height;
cyl_volume= (pi.*radius .^2) .* height;

>> radius = 4; height = 5;


>> [ cyl_area , cyl_volume] = cylinder1(radius, height)
cyl_area =
125.6637
cyl_volume =
251.3274
User-Defined Functions
>> clear ;
>> [ A, V] = cylinder1(4, 5)
A=
125.6637
V=
251.3274

What are the outputs of the followings:

>> cyl_area >> cyl_volume

>> radius >> height


User-Defined Functions

>> [A V] = cylinder1(4, [1:2:10])

A=

V=
Global Variable
To use a variable as global (available in function/s and Command
Window), global command can be used. To realize this let’s create the
following area_circumf .m function file

function [area, circumf] = area_circumf (radius)


%function to calculate area and circumference of a circle
global area circumf
area= (pi.*radius.^2);
circumf= (2.*pi.*radius);

>> [a c] = area_circumf(4)
a=
50.2655

c=
25.1327
Global Variable (cont.)
>> area
<error>

>> circumf
<error>

>> global area cicumf;


>> [area circumf]
ans=

50.2655 25.1327
Global Variable (cont.)
Lets modify the cylinder1.m file and save it as sub_cylinder.m
function [cyl_ area,cyl_volume]=sub_cylinder(height)
%function to calculate area and volume of a cylinder
global area circumf
cyl_area= circumf.*height;
cyl_volume= area.*height;

>> [ A, V] = sub_cylinder(5)
A=
125.6637
V=
251.3274
Subfunction
A function can have subfunction/s within its M-file. For example the
function cylinder can be form from function sub_cylinder and
function area_circumf (without using the global variables).
function [cyl_area,cyl_volume]=cylinder2(cir_radius,height)
%function to calculate area and volume of a cylinder
[cir_area,cir_circumf]=circle(cir_radius)
cyl_area= cir_circumf.*height;
cyl_volume= cir_area.*height;

function [a_rea,c_ircumf]=circle(radius)
%function to calculate circle area and circumference
a_rea = pi.*radius.^2;
c_ircumf = 2.*pi.*radius;
Anonymous Function
Annonymous function is a simple function created without the need
to create a M-file. It can be created in Command Window or within
another function or script. The syntex for anonymus function is:
fhandle - @(input_args) expression.

Where fhandle is the function handle variable, expression is any


single valid Matlab expression and input_args are consist of all the
variables used in the expression.
>> y = @(a,b ) 5 * a + b^2;
>> y (2,4)
ans =
26
Data Files
 To import ASCII data into Matlab, the data should first be edited
outside Matlab environment or using the Import wizard (File
>Import Data… <choose file>). Any data header should be
removed and commas (if exist) should be replaced by spaces.
 Data file can have any extension except .mat.
 If the data file has m lines and n values in each lines, the data will
be assigned to an m × n matrix of the variable name same as the
file name. (load filename)
 Microsoft excel workbook file (*.xls) can be easily imported using
the xlsread(‘filename’) command
>> A= xlsread(‘data’)
>> [A,B]=xlsread(‘data’) – array A will contains the workbook data
while array B will contains all text
Data Files (cont.)
 To export Matlab matrix as ASCII data file where rows and
columns are represented as space-delimited numeric values used
either Matlab command save-ACII or dlmwrite function
>> data1=[1,2,3,4;5,6,7,8];
>> save data_out.dat data1 –ASCII

 Try the followings


>>dlmwrite('example.dat',data1)
>>dlmwrite('example.dat',data1,'-append')
>>dlmwrite('example.da’,data1,'delimiter','\t','precision','%.6f')
>>dlmwrite('example.dat',data1,'newline','pc')
>>dlmwrite('example.dat',data1,'delimiter','\t','newline','pc')

You might also like