Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained.

Slides:



Advertisements
Similar presentations
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Advertisements

Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements.
COMP 116: Introduction to Scientific Programming Lecture 37: Final Review.
M AT L AB Programming: scripts & functions. Scripts It is possible to achieve a lot simply by executing one command at a time on the command line (even.
Flow Charts, Loop Structures
CMPS 1371 Introduction to Computing for Engineers
Programming with MATLAB
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Lecture 7 Sept 29 Goals: Chapters 5 and 6. Scripts Sequence of instructions that we may want to run can be stored in a file (known as script). by typing.
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
Programming in MATLAB Week 14 – 4/28/09 Kate Musgrave
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/29/2003.
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.
M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files called M- files. M-files are.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
Matlab Programming, part 1 M-files It is generally more convenient to program in Matlab using m-files, ascii text files containing a set of Matlab commands.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
MATLAB Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Flow Control and Functions ● Script files ● If's and For's ● Basics of writing functions ● Checking input arguments ● Variable input arguments ● Output.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
CSE123 Lecture 3 Files and File ManagementScripts.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
General Computer Science for Engineers CISC 106 Lecture 15 Dr. John Cavazos Computer and Information Sciences 03/16/2009.
Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained.
SCRIPTS AND FUNCTIONS DAVID COOPER SUMMER Extensions MATLAB has two main extension types.m for functions and scripts and.mat for variable save files.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
EEE 161 Applied Electromagnetics
Repetition Structures Chapter 9
MATLAB: Script and Function Files
Matlab Training Session 4: Control, Flow and Functions
Topics Introduction to Repetition Structures
Scripts & Functions Scripts and functions are contained in .m-files
Outline Matlab tutorial How to start and exit Matlab Matlab basics.
MATLAB: Structures and File I/O
User Defined Functions
Exception Handling Chapter 9.
T. Jumana Abu Shmais – AOU - Riyadh
Matlab Training Session 5: Importing Data
Python programming exercise
Note on Indexing of Array Elements
Introduction to MATLAB
Functions continued.
Presentation transcript:

Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained in m-files, MATLAB needs to know where they are! store in working directory store in any directory, add to MATLAB's path

To check/change working directory pwd, cd, ls, dir menu bar current directory window To check/modify path path addpath, rmpath, savepath File >> Set Path... Reminders

Scripts No input/output Operate on pre-defined workspace variables and variables defined in script Variables created during script are saved in workspace (side effects) Kind of like macros, relatively inflexible → good for executing repeated long/complicated sequences of commands on same data

How to execute a script Save list of commands, variable definitions, etc in plain text file 'scriptEx.m' Make sure 'scriptEx.m' is saved in wd, OR, its directory is added to path type 'scriptEx' at command prompt (no quotes) Be aware of side effects

Functions Basically, scripts that take input and create some output Operate on input variables and variables defined in function Variables created during function call only exist as long as function is running can be saved to workspace if defined as output → good for performing general tasks with optional parameters on different datasets

How to call your function (1) Write it (we'll get to that), and save as plain text file 'functionEx.m' Make sure 'functionEx.m' is saved in wd, OR, its directory is added to path Let's assume your function has 2 input arguments, these could be input data or parameters to pass to function body Examples...

How to call your function (2) Arg1 is data stored as workspace variable, Arg2 is file containing data, or could be a file to create for output >> functionEx(x, 'filename.txt') Arg1 is another function, Arg2 is scalar parameter >> 1000) >> functionEx('func', 1000)

Defining your function First line must contain the following 'function' command name(s) of output, if any name of function, should be same as name of m-file name(s) of input parameters/arguments function output = userFunc(arg1, arg2) function [ ] = userFunc(arg1, arg2, arg3) function [out1, out2] = userFunc(arg1)

What about lines 2,3,4,...? Function body can contain anything, as long as it's a valid MATLAB statement/command Any text preceded by a % is not executed → % this is a comment If function definition on line 1 is followed by block of commented lines, comments will be printed as help file when user types >> help userFunc

Programming basics Loops for loop → perform command(s) for some predetermined amount of times while loop → perform command(s) as long as some condition is met If-then statements if some condition is met → perform command(s) else and elseif

For loop While loop for index = 1:n do something end for i = 1:5 i end while condition do something end j = 1; while j <= 5 j j = j+1; end

For loops Avoid hogging memory, use vector alternatives in MATLAB vec = [2:2:10] for i = 1:length(vec) vec(i) = vec(i) + 10; end vec = vec = vec + 10

How to loop through a matrix m = [1:4; 5:8; 9:12]; % create 3x4 matrix, values % 1:12 [rows cols] = size(m); % store # of rows and columns % in variables for rowstep = 1:rows % loop through rows for colstep = 1:cols % loop through columns rowstep % print current row colstep % print current column m(rowstep, colstep) % print value at current % position end % close column loop end % close row loop

Turn that script into a function... function [] = read_matrix(m) % % function [] = read_matrix(m) % % HELPFILE for trivial function 'read_matrix.m' % % 'm' is a matrix % elements of 'm' will be read starting with [1,1] % % at each step, indices and their corresponding value % will be printed to the screen % START CODE [rows cols] = size(m); % store # of rows and columns in variables for rowstep = 1:rows % loop through row numbers for colstep = 1:cols % loop through column numbers rowstep % print current row colstep % print current column m(rowstep, colstep) % print value at current position end % close column loop end % close row loop

If statements if condition do something end if condition do something else do something end val = m(3,2) if val < 5 val = 0; elseif val > 5 val = val * 10; else val = val^2; end

File handling >>help iofun >>help fileformats load dlmread, dlmwrite xlsread, xlswrite urlread, urlwrite unzip, untar

More file handling, C style fopen, fclose fgetl, fscanf, fseek, ftell fid = fopen('mean.m'); % open file for reading while 1 % start loop, basically will loop % forever unless it is stopped line = fgetl(fid); % get next line in 'fid' if ~ischar(line) % if 'line' is empty break loop break end disp(line) % print the current line end fclose(fid); % close file

Let's put this all to use...

Example: transition probabilities Probabilistic sequence of events We have multiple examples of this sequence Given a particular event, how reliably can we predict the next event (including the end)? Assume sequence is always same number of events long Represent each event with a number Ex. Sequence: 1 – 1 – 2 – 3 – 3 – 3 – 1 – 4 – 4 – 4

Basic idea Represent each sequence as series of integers Collect a bunch of these sequences and put them in a matrix where each row is one example of the sequence We'll write code that will take the matrix and two of the events as arguments Code will return a probability: How often one event follows the other

Some sequences

Example: transition probabilities Givens A matrix 'data' containing events/numbers, each row is a sequence two events, 'leader' and 'follower' Tasks Compute how often 'follower' immediately follows 'leader' Compute how often 'leader' ends the sequence, i.e. is the last number in its row

Strategy Loop through matrix, looking for 'leader' Check to see if it is at the end of the row if yes, tally 1 Check to see if the next element is 'follower' if yes, tally 1 Count total number of times 'leader' occurs Total up the tallies and divide each by the total occurrences of 'leader'

Implement in a function function [prob term_prob] = transProbEXAMPLE(filename,leader,follower) % % function [prob term_prob] = transProbEXAMPLE(filename,leader,follower) % % 'filename' is plain text file containing sequences stored in a matrix % % 'leader' and 'follower' are events/numbers of interest % % This function will compute transition probability from 'leader' to % 'follower' % i.e. how often 'leader' is immediately followed by 'follower' % % returns 'prob' - 'leader' -> 'follower' transition probability % 'term_prob' - probability that 'leader' is last element % in its row

read in data and define variables... data = dlmread(filename); % pass info in 'filename' to variable 'data' % initialize empty counting vectors track_trans = []; % for tracking instances when 'leader' is % followed by 'follower' track_terms = []; % for tracking instances when 'leader' % terminates the line [data_row, data_col] = size(data); % save dimensions of 'data' % to use for looping

do the work... % loop over 'data' rows for row = 1:data_row % loop over 'data' columns for col = 1:data_col % look for instances of 'leader' if data(row, col) == leader % check if 'leader' terminates line if col == data_col % if yes, add 1 to counting vector track_terms = [track_terms 1]; % see if next element is 'follower' elseif data(row, col + 1) == follower % if yes, add 1 to counting vector track_trans = [track_trans 1]; end

do the work... % loop over 'data' rows for row = 1:data_row % loop over 'data' columns for col = 1:data_col % look for instances of 'leader' if data(row, col) == leader % check if 'leader' terminates line if col == data_col % if yes, add 1 to counting vector track_terms = [track_terms 1]; % see if next element is 'follower' elseif data(row, col + 1) == follower % if yes, add 1 to counting vector track_trans = [track_trans 1]; end

do the work... % loop over 'data' rows for row = 1:data_row % loop over 'data' columns for col = 1:data_col % look for instances of 'leader' if data(row, col) == leader % check if 'leader' terminates line if col == data_col % if yes, add 1 to counting vector track_terms = [track_terms 1]; % see if next element is 'follower' elseif data(row, col + 1) == follower % if yes, add 1 to counting vector track_trans = [track_trans 1]; end

do the work... % loop over 'data' rows for row = 1:data_row % loop over 'data' columns for col = 1:data_col % look for instances of 'leader' if data(row, col) == leader % check if 'leader' terminates line if col == data_col % if yes, add 1 to counting vector track_terms = [track_terms 1]; % see if next element is 'follower' elseif data(row, col + 1) == follower % if yes, add 1 to counting vector track_trans = [track_trans 1]; end

compute outputs... % count total number of instances of 'leader' total_leader = length(find(data == leader)); % count number of times 'follower' followed 'leader' count_trans = sum(track_trans); % count number of times 'leader' terminated line terminations = sum(track_terms); % calculate probability that 'follower' follows 'leader' prob = count_trans/total_leader; % calculate probability that 'leader' terminates line term_prob = terminations/total_leader;

compute outputs... % count total number of instances of 'leader' total_leader = length(find(data == leader)); % count number of times 'follower' followed 'leader' count_trans = sum(track_trans); % count number of times 'leader' terminated line terminations = sum(track_terms); % calculate probability that 'follower' follows 'leader' prob = count_trans/total_leader; % calculate probability that 'leader' terminates line term_prob = terminations/total_leader;

compute outputs... % count total number of instances of 'leader' total_leader = length(find(data == leader)); % count number of times 'follower' followed 'leader' count_trans = sum(track_trans); % count number of times 'leader' terminated line terminations = sum(track_terms); % calculate probability that 'follower' follows 'leader' prob = count_trans/total_leader; % calculate probability that 'leader' terminates line term_prob = terminations/total_leader;

Extra credit Generalize this function to deal with sequences of variable length Hint: more of a data representation problem than a coding problem can't have a matrix with rows of different lengths use 'NaN' to fill space, >>help isnan still want to know if 'leader' is last event in sequence Only need a couple extra lines of code

Extra credit Could also use C style file handling functions fopen, fgetl, etc to read one line at a time instead of loading whole data file at once into matrix Then no problems with unmatched row lengths etc, but would need more changes to code