advanc e d mat l ab
play

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.


  1. 2017‐07‐24 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. commands exactly as you • A Function starts with the would type them at the keyword function command line. • function out1, out2,.. = • Scripts are simply lines of funName(arg1, arg2,..) codes some lines of code… • We use semi colon ‘;’ to control the output return out1, out2, … end 1

  2. 2017‐07‐24 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 2

  3. 2017‐07‐24 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 • We can use the if statement only statement for flow control • use the if – elseif—else—end • The syntax is: syntax can improve coding efficiency if [logical statement] • The program run from the top to codes … the bottom, if a true logical statement is found, the program elseif [logical statement] will skip the rest codes… • The cadToUsdTwoDirection function else codes… end 3

  4. 2017‐07‐24 T he ite ratio n / lo o p struc ture • the while loop • the for loop • the loop repeats when • the loop repeats specified condition is true number of times while logical statement for index = values codes… codes… end end MAT L AB Data struc ture • Cell array • Structure array • Cell array can take different types of • Structure array can hold different data and be called by indices types of data and called by their names / attributes • CellArray_1= cell(n) • A structure array uses the ‘.’ to • CellArray_2=cell(sz1,…,szN) deference the value • CellArray_3=cell(sz) • str_01 = struct • str_02 = struct(field, value) • str_03 = struct(fields, values) • str_04 = struct([]) 4

  5. 2017‐07‐24 T able in MAT L AB • A table is a data spread sheet in • create a table from workspace MATLAB like a worksheet in an variables Excel workbook. • create a table and specify • t01 = table(var1, …, varN) variable names – all variables must have the save • read / write a table from an number of rows external CSV file by command • t02 = table(var1, …, varN, Name, • read a table from an external value) CSV file by menu clicks – use Name—value pairs to specify row / column names • t03 = table() – an empty table Handling missing data • NaN behavior ‐‐ Not a Number • two ways • NaN Removal • remove the NaN elements – it reduces the size of your data, can • replace by other values (should be harmful is the data set need to do the least harm) 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 5

  6. 2017‐07‐24 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 of 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 • data=input( ʹ Enter number of to interact with users hours: ʹ ) • input with Text prompt • name=input( ʹ Enter the employee name: ʹ , ʹ s ʹ ); ‐‐ the • Text Output input must be a string • Write data on the screen • disp( ʹ MATLAB Rocks ʹ ) • Display warning message • fprintf( ʹ Cost = $%d\n ʹ ,20) • Display error message 6

  7. 2017‐07‐24 Cre ate simple GUI • menu( ) ‐‐ Generate menu of choices for user • color = menu( ʹ Choose a color ʹ , input. ʹ red ʹ , ʹ blue ʹ ) • menu returns the number of the selected • value = inputdlg( ʹ Please enter a menu item, or 0 if the user clicks the close value: ʹ ) button. • filename = uigetfile • Creates a modal dialog box and returns user input in a cell array. • msgbox( ʹ Analysis complete ʹ ) • Open standard dialog box for retrieving • warndlg( ʹ Non ‐ integer values files. Returns the file name as a character array ‐‐ return file name ONLY, without path rounded. ʹ ) • Gathers n inputs from mouse clicks. • errordlg( ʹ Data not found ʹ ) • Gathers n inputs from mouse clicks • Display a message box • Display a waiting bar • Display warning / error dialog box 7

Recommend


More recommend