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

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.
Programming with MATLAB
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
Scripts and Flow Control. Scripts So far we have been entering commands directly into the command line But there is a better way Script files (and functions)
True BASIC Ch. 6 Practice Questions. What is the output? PRINT X LET X = -1 PRINT X FOR X = 4 TO 5 STEP 2 PRINT X NEXT X PRINT X END.
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 17 Goals: Complete Chapter 4 Chapters 5 and 6.
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
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.
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.
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
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.
Python Programming Fundamentals
MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
Chapter Nine Advanced Shell Scripting1 System Programming Advanced Shell Scripting.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
How to think through your program [ principles of good program design ] Rachel Denison MATLAB for Cognitive Neuroscience ICN, 13 December 2007.
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.
Compiled Matlab on Condor: a recipe 30 th October 2007 Clare Giacomantonio.
Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Lecture 4  C Shell Scripts(Chapter 10). Shell script/program  Shell script: a series of shell commands placed in an ASCII text file  Commands include.
Linux Operations and Administration
Lecture 7 Sept 22 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.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
Introduction to Perl Yupu Liang cbio at MSKCC
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Script M-Files Group of Matlab commands placed in a text file with a text editor. Matlab can open and execute the commands exactly as if they were entered.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
User-defined Matlab functions. Creating function m-files with a plain text editor MATLAB m-files must be plain text files. Most word-processors provide.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming.
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.
A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.
Matlab Programming a logical approach. Outline Announcements: –Homework I: due Wed. by 5, by –Last day to add/drop or change credit/audit Iteration.
Introduction to MATLAB Section2, statistics course Third year biomedical dept. Dina El Kholy, Ahmed Dalal.
Introduction to Unix (CA263) Command File By Tariq Ibn Aziz.
General Computer Science for Engineers CISC 106 Lecture 15 Dr. John Cavazos Computer and Information Sciences 03/16/2009.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
General Computer Science for Engineers CISC 106 Lecture 03 James Atlas Computer and Information Sciences 6/15/2009.
User Defined Functions Spring EE 201. Class Learning Objectives  Achieve Comprehension LOL of User Defined Functions. Spring
INTRODUCTION TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in.
Matlab Programming for Engineers
EEE 161 Applied Electromagnetics
Matlab Training Session 4: Control, Flow and Functions
Shell Scripting March 1st, 2004 Class Meeting 7.
Basic operations in Matlab
Scripts & Functions Scripts and functions are contained in .m-files
User Defined Functions
Please use speaker notes for additional information!
CS100J 26 April. Matlab Use help button!!! Variables, values, types
Writing functions in MATLAB
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Python programming exercise
Functions continued.
Using Script Files and Managing Data
Scripts In Matlab.
ME 123 Computer Applications I Lecture 7: Basic Functions 3/20/03
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 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 >> 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

For loop

While loop

If statement

File handling >>help iofun