140 likes | 300 Views
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. Reminders.
E N D
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
Reminders • 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...
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 other data • >> functionEx(x, 'filename.txt') • Arg1 is data, Arg2 is desired name of output file • >> functionEx(x, 'output.txt') • Arg1 is another function, Arg2 is scalar parameter • >> functionEx(@func, 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 a set of commands for some predetermined amount of times • while loop → perform set of commands as long as some condition is met • If-then statements • if some condition is met → perform set of commands • else and elseif
File handling • >>help iofun