Advanc e d MAT L AB Stanley Liang, PhD York University MAT L AB - - PDF document

advanc e d mat l ab
SMART_READER_LITE
LIVE PREVIEW

Advanc e d MAT L AB Stanley Liang, PhD York University MAT L AB - - PDF document

20170724 Advanc e d MAT L AB Stanley Liang, PhD York University MAT L AB Sc ript vs. MAT L AB F unc tio n Scripts are the simplest type of Functions are more flexible program, since they store and more easily extensible.


slide-1
SLIDE 1

2017‐07‐24 1

Advanc e d MAT L AB

Stanley Liang, PhD York University

MAT L AB Sc ript vs. MAT L AB F unc tio n

  • Scripts are the simplest type of

program, since they store commands exactly as you would type them at the command line.

  • Scripts are simply lines of

codes

  • We use semi colon ‘;’ to control

the output

  • Functions are more flexible

and more easily extensible.

  • A Function starts with the

keyword function

  • function out1, out2,.. =

funName(arg1, arg2,..) some lines of code… return out1, out2, … end

slide-2
SLIDE 2

2017‐07‐24 2

F unc tio n Syntax

  • function [y1,...,yN] = myfun(x1,...,xM)
  • start with key work ‘function’
  • put all the output on the left of the “=“ side, i.e. all Ys
  • on the right, give an appropriate name to the function
  • use a parenthesis to include all the input arguments, i.e.

all the Xs

A func tio n to c o mpute the triang le are a

  • function to compute the area of a triangle for specific base and

height function y = triangleArea(base, height) y = 0.5 *base*height end

  • Note that the return key word in MATLAB only ends the function
  • The return key word does not return a value, because a MATLAB

function can return many outputs

slide-3
SLIDE 3

2017‐07‐24 3

A mo re c o mple x func tio n to c o mpute the are a and the pe rime te r o f a c irc le with a g ive n radius

function [area, perimeter] = circleFun(radius) area = pi*radius^2 perimeter = 2*pi*radius return

  • to get the area

– [area, ~] = circleFun(3)

  • to get the perimeter

– [~,perimeter] = circleFun(3)

  • to get both

– [area, perimeter] = circleFun(3)

T he branc hing struc ture – if – e lse if - e lse

  • MATLAB use the if, elseif, else

statement for flow control

  • The syntax is:

if [logical statement] codes … elseif [logical statement] codes… else codes… end

  • We can use the if statement only
  • use the if – elseif—else—end

syntax can improve coding efficiency

  • The program run from the top to

the bottom, if a true logical statement is found, the program will skip the rest

  • The cadToUsdTwoDirection

function

slide-4
SLIDE 4

2017‐07‐24 4

T he ite ratio n / lo o p struc ture

  • the while loop
  • the loop repeats when

condition is true while logical statement codes… end

  • the for loop
  • the loop repeats specified

number of times for index = values codes… end

MAT L AB Data struc ture

  • Cell array
  • Cell array can take different types of

data and be called by indices

  • CellArray_1= cell(n)
  • CellArray_2=cell(sz1,…,szN)
  • CellArray_3=cell(sz)
  • Structure array
  • Structure array can hold different

types of data and called by their names / attributes

  • A structure array uses the ‘.’ to

deference the value

  • str_01 = struct
  • str_02 = struct(field, value)
  • str_03 = struct(fields, values)
  • str_04 = struct([])
slide-5
SLIDE 5

2017‐07‐24 5

T able in MAT L AB

  • A table is a data spread sheet in

MATLAB like a worksheet in an Excel workbook.

  • t01 = table(var1, …, varN)

– all variables must have the save number of rows

  • t02 = table(var1, …, varN, Name,

value)

– use Name—value pairs to specify row / column names

  • t03 = table() – an empty table
  • create a table from workspace

variables

  • create a table and specify

variable names

  • read / write a table from an

external CSV file by command

  • read a table from an external

CSV file by menu clicks

Handling missing data

  • NaN behavior ‐‐ Not a Number
  • NaN Removal

– it reduces the size of your data, can be harmful is the data set need to be merged with others in the same length – When NaN elements exist in your data, they can prevent you from performing meaningful calculations. – For example, mean([1,2,3,NaN,5]); returns NaN

  • two ways
  • remove the NaN elements
  • replace by other values (should

do the least harm)

slide-6
SLIDE 6

2017‐07‐24 6

I de ntify missing data

  • Many plotting functions treat NaN

elements appropriately as missing elements.

  • In some cases, it is better to leave NaN

elements in your data set when plotting, since doing so illustrates the missing data.

  • use the nnz function to count the number
  • f missing data points
  • Interpolating missing Data

– Piecewise Interpolation: values are predicted based on only the neighboring data points

Simple use r inte rac tio ns

  • MALTAB can create simple UI

to interact with users

  • input with Text prompt
  • Text Output
  • Write data on the screen
  • Display warning message
  • Display error message
  • data=input(ʹEnter number of

hours: ʹ)

  • name=input(ʹEnter the

employee name: ʹ,ʹsʹ); ‐‐the input must be a string

  • disp(ʹMATLAB Rocksʹ)
  • fprintf(ʹCost = $%d\nʹ,20)
slide-7
SLIDE 7

2017‐07‐24 7

Cre ate simple GUI

  • menu( ) ‐‐ Generate menu of choices for user

input.

  • menu returns the number of the selected

menu item, or 0 if the user clicks the close button.

  • Creates a modal dialog box and returns user

input in a cell array.

  • Open standard dialog box for retrieving
  • files. Returns the file name as a character

array ‐‐ return file name ONLY, without path

  • Gathers n inputs from mouse clicks.
  • Gathers n inputs from mouse clicks
  • Display a message box
  • Display a waiting bar
  • Display warning / error dialog box
  • color = menu(ʹChoose a colorʹ,

ʹredʹ,ʹblueʹ)

  • value = inputdlg(ʹPlease enter a

value:ʹ)

  • filename = uigetfile
  • msgbox(ʹAnalysis completeʹ)
  • warndlg(ʹNon‐integer values

rounded.ʹ)

  • errordlg(ʹData not foundʹ)