Академический Документы
Профессиональный Документы
Культура Документы
FUNCTION IN MATLAB
Function in MATLAB
There are two main types of function in
MATLAB
i. Built-in function
ii. User defined function
Built-in function
Examples of built-in functions
sin
(sine)
e.g sin(x) x in
radian
cos (cosine)
abs (absolute value)
exp (exponential)
User-defined function
MATLAB allows users to define their
own function
Function allows a large problem to be
broken down to smaller problems
Function allows many statements or
commands that perform a task to be
grouped together in a separate file
The function can be called repeatedly
in a program or script
User-defined function
In MATLAB, functions are also called
M-file function because they are
stored in separate M-files
A user-defined function can accept
one or many inputs. Inputs are called
arguments
A user-defined function can return no
value, one value, or many values
Structure of a function
function [output arguments] = functionname(input arguments)
% comment describing the function
Statements here. This must include assigning values to all
output arguments
end
Save as myclearscreen.m
Type myclearscreen in the
Command Window
Save as myaddition.m
function sum =
myaddition(a,b)
Type
myaddition(2,3)
or type
a=2
b=3
total = myaddition(a,b)
Primary function
A primary function is a function that
is defined and saved in a separate
file
A primary function can be called by
any other function or script
All the previous examples are
primary functions
Local function
Local function is also called subfunction
Local function is defined within the
same file as the primary function
Local function is valid only within the
file that defines it
Anonymous function
Anonymous function is a function that is
not stored in a program file
It is associated with a variable whose data
type is function_handle
Example
sqr = @(x) x.^2;
a = sqr(5)
a=
25
Nested function
A nested function is a function that is
completely contained within a parent
function.
Any function in a program file can
include a nested function.
Nested function can access and
modify variables that are defined in
their parent functions.
end
SELECT SPREADSHEET
DATA USING IMPORT TOOL
IMPORT A WORKSHEET OR
RANGE
Read Column-Oriented Data into Table
Read Numeric and Text Data into Arrays
Get Information about a Spreadsheet
Read Column-Oriented
Data into Table
Temperatureres.
INTRODUCTION
MAT-files
Text (ASCII) data
Spreadsheets
Scientific data
Images
Audio and video
Data in extended markup language (XML)
Data created using the Data Acquisition Toolbox (see daq
The easiest way to import data into MATLAB from a disk fil
the
system clipboard is to use the Import Wizard, a graphical u
interface.
The Import Wizard helps you find a file and define the vari
to
use.
in the MATLAB workspace.
CAUTION!!!
When you import data into the MATLAB workspace, the new
variables you create overwrite any existing variables in the
That have the same name
.
IMPORT DATA
To import data from a file, start the Import Wizard, using on
methods:
Select File > Import Data.
Double-click a file name in the Current Folder browser.
Click the Import Data button in the Workspace browser.
To import data from the clipboard, start the Import Wizard,
these methods:
Select Edit > Paste to Workspace.
THANK YOU =)