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

GROUP 7:

Function, Examples and How To Select Spreadsheet Data


Using Import Tool, Import A Worksheet or Range, Import
All Worksheets From A File, Import & Export Data To
Excel
PRESENTERS:
Muhammad Mustaqem Bin Shukri
Muhammad Mikhail Bin Hasanuddin
Nor Khalilah Binti Kamis
Nurul Fatihin Binti Rosjelani
Fatin Elida Binti Faslee

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 are functions that are


provided by MATLAB
Type help at the prompt in Command
Window to show help topic
For example, choose matlab\elfun. It will
show a list of elementary math 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 the function in a file with the same name as the


functionname
The file must be in the same folder as the main script

Example: return no value


function myclearscreen()
% This function clears the Command
Window
clc
end

Save as myclearscreen.m
Type myclearscreen in the
Command Window

Example: return 1 value


function sum = myaddition(a,b)
% This function receives two numbers
and returns the sum of the two numbers
sum = a + b
end

Save as myaddition.m

function sum =
myaddition(a,b)
Type
myaddition(2,3)

or type
a=2
b=3
total = myaddition(a,b)

The value of sum is assigned to


total

Example: receive and return


more than 1 value
function (sum, average) = sum_and_average(a,b,c)
% This functions receives 3 numbers and returns
the
% sum and the average values
sum = a + b + c
average = sum / 3
end

Save the function in a file


sum_and_average.m

function (sum, average) =


sum_and_average(a,b,c)
Type in Command Window
[s, av] = sum_and_average(3, 5, 8)
The value of sum is assigned to s
The value of average is assigned to
av

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

Example: Local function


function print_sum (a, b)
% This is the primary function. It calls a local function
% add_numbers, then print the sum of two numbers
sum = add_numbers(a,b)
fprintf('The sum of three numbers = %f\n', sum)
end

function total = add_numbers(x, y)


% This is the local function
total = x + y
end

print_sum is the primary function


add_numbers is the local function
Save the whole thing in a file print_sum.m

function print_sum (a, b)


Type
print_sum(2,3)

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

sqr ia a variable of type function_handle


The @ operator creates the handle

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.

Example: Nested function


function parent
disp('This is the parent function')
nestedfx
function nestedfx
disp('This is the nested function')
end

end

SELECT SPREADSHEET
DATA USING IMPORT TOOL

1)Press the Import button.

2)Select the file you


want to import.
(.xls, .xlsx, .xlsb,
or .xlsm)

In the Imported Data Section, select how you want


the data to be imported.

Column vectors - Import each column of the


selected data as an individual m-by-1 vector.
Matrix -Import selected data as an m-by-n
numeric array.
Cell Array -Import selected data as a cell array
that can contain multiple data types, such as
numeric data and text.

Three columns of data;


Date
Temperature
Station

When the Column Vector option is


chosen .
The ttring character, x is replaced to
NaN(Non-Numeric)

You can add, remove, reorder,


or edit rules,.

Such as changing the

Replacement value from NaN to another value.

All rules apply to the imported data only, and do not


change the data in the file.
You must specify rules any time the range includes
nonnumeric data and you are importing into a matrix
or numeric column vectors.

Any cells that contain #Error?


Correspond to formula errors in
your spreadsheet file.
Such as division by zero.
The Import Tool
regards these cells as
nonnumeric.

Import button is disabled.

So, we could exclude all rows


or all columns with the
Unimportable cells

When you click the Import Selection button , the Import


Tool creates variables in your workspace.

For multiple imports, we use the function


1)Open one of the files in the Import Tool.
2)From the import button, press
generate function.
3) Code opened in editor;
function data = importfile(workbookFile, sheetName, range)
%IMPORTFILE Import numeric data from a spreadsheet
4)Copy the code and save
the function.

In the command window,

paste the code.

All the variables are imported


into the workspace.

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

How to import mixed numeric


and text data from a
spreadsheet into a table, using
the readtable function.
Tables are suitable for column-oriented
or tabular data.
We can store variable names or row
names along with the data in a single
container.

Using a sample spreadsheet file,


climate.xlsx, that contains the following
numeric and text data in a worksheet called
Temperatures.

Creating the sample file for reading.

xlswrite warns that it has added a


worksheet.
Call readtable to read all the data in the
worksheet called Temperatures.

By default, readtable reads the first row


of the worksheet as variable names for
the table.
Read only the first two columns of data by
specifying a range, 'A1:B4'.

readtable returns a 3-by-2 table.

Read Numeric and Text


Data
into
Arrays
How to import mixed numeric and
text data into separate arrays in
MATLAB, using the xlsread
function.
Using a sample spreadsheet file,
climate.xlsx, that contains the following
data in a worksheet called

Temperatureres.

Create the sample file for reading.

xlswrite warns that it has added a


worksheet.

Import only the numeric data into a matrix,


using xlsread with a single output
argument. xlsread ignores any leading row
or column of text in the numeric result.

xlsread returns the numeric array, num.

Alternatively, import both numeric data and


text data, by specifying two output
arguments in the call to xlsread.

xlsread returns the numeric data in the


array, num, and the text data in the cell
array, headertext.

Read only the first row of data by specifying


a range, 'A2:B2'.

Get Information about a


Spreadsheet

To determine whether a file contains a


readable Excel spreadsheet, use the xlsfinfo
function.
For readable files, xlsfinfo returns a nonempty
string, such as 'Microsoft Excel
Spreadsheet'.
Otherwise, it returns an empty string ('').
Can also use xlsfinfo to identify the names of
the worksheets in the file, and to obtain the
file format reported by Excel.

For example, retrieve information about the


spreadsheet file, climate2.xlsx, created in
the previous example:

IMPORT ALL WORKSHEET


FROM FILE

INTRODUCTION

MATLAB includes functions tailored to import these specific

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

Tools that Import Multiple File Formats

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.

Viewing the Contents of a File

For text files, the Import Wizard automatically displays a preview


of the data in the file.
To view images or video, or to listen to audio, click the Back but
on the first window that the Import Wizard displays.

The right pane of the new window includes a preview tab.


Click the button in the preview tab to show an image or to play
audio or video.

IMPORT & EXPORT DATA


FROM EXCEL

It is important to be able to import


and export data to and from Excel in
order to compare analytical and
experimental results.
This steps will address this issue and
discuss methods to solve it.

IMPORT DATA FROM EXCEL

First, open Matlab application.

Second, click on Open and select


folder where excel file is located.

Third, choose to show All files.


Select the excel file and click Open.

Fourth, in the newly opened window


you can select the part of the
document and import it by pressing
on "Import Selection".

File will appear on Workspace. Now


you can use your data from the excel
document in Matlab.

EXPORT DATA TO EXCEL

First Step: File naming


Syntax for file name:
filename=
.xlsx;
*the blank space is for the desired file
name

Second Step: Create a set of data/array


Syntax : (variable name)=[ ];
*variable name can be represent in
alphabets

Third Step: Exporting data to excel


Syntax : xlswrite(filename,X)

Initially inside current folder, there is no


excel file existed.
After successfully export matlab data to
excel,the file will automatically create in
your current folder and appear on your
current folder bar.

THANK YOU =)

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