1 / 14

Extending MATLAB

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.

elton
Télécharger la présentation

Extending MATLAB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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...

  3. 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

  4. 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

  5. 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

  6. 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 ...

  7. 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)

  8. 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)

  9. 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

  10. 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

  11. For loop

  12. While loop

  13. If statement

  14. File handling • >>help iofun

More Related