McLab Tutorial www.sable.mcgill.ca/mclab Part 2 – Introduction to MATLAB Functions and Scripts Data and Variables Other Tricky "Features" 6/4/2011Matlab-

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
CPSC 388 – Compiler Design and Construction
Introduction to Matlab
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
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.
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
Programming Environment S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: Cells and Structures.
McLab Tutorial Part 8 – Wrap Up Summary Ongoing and Future Work Further Sources 6/4/2011Wrap Up- 1McLab Tutorial, Laurie Hendren,
Chapter 5 ( ) of Programming Languages by Ravi Sethi
Chapter 7: User-Defined Functions II
Compiler Construction
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/22/2003.
MATLAB Cell Arrays Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University.
MATLAB and Simulinklecture 31 To days Outline  Functions  Strings  Sparse Arrays  Cell Arrays  Structures  Exercises on this days topics.
C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB
Review of C++ Programming Part II Sheng-Fang Huang.
McLab Tutorial Laurie Hendren, Rahul Garg and Nurudeen Lameed Other McLab team members: Andrew Casey, Jesse Doherty, Anton Dubrau,
Static checking and symbol table Chapter 6, Chapter 7.6 and Chapter 8.2 Static checking: check whether the program follows both the syntactic and semantic.
Operator Overloading and Type Conversions
McLab Tutorial Part 6 – Introduction to the McLab Backends MATLAB-to-MATLAB MATLAB-to-Fortran90 (McFor) McVM with JIT 6/4/2011Backends-
MATLAB Lecture One Monday 4 July Matlab Melvyn Sim Department of Decision Sciences NUS Business School
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
McLab Tutorial Part 4 – McLab Intermediate Representations High-level McAST Lower-level McLAST Transforming McAST to McLAST 6/4/2011IR-
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
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.
McLab Tutorial Part 5 – Introduction to the McLab Analysis Framework Exploring the Main Components Creating a Simple Analysis.
Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Compiler Construction
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
McLab Tutorial Part 3 – McLab Frontend Frontend organization Introduction to Beaver Introduction to JastAdd 6/4/2011 Frontend-1McLab.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Introduction to Matlab By E. Noura Semary. Contents MATLAB Environment  Command window, Workspace, Path window, Editor window,and Figure window) Basic.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CSE 455 : Computer Vision MATLAB 101 Getting Started with MATLAB.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
SCRIPTS AND FUNCTIONS DAVID COOPER SUMMER Extensions MATLAB has two main extension types.m for functions and scripts and.mat for variable save files.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
Sundermeyer MAR 999 Spring Laboratory in Oceanography: Data and Methods MAR599, Spring 2009 Miles A. Sundermeyer Data Handling in Matlab.
Java Programming Language Lecture27- An Introduction.
Matlab Programming for Engineers
Java Primer 1: Types, Classes and Operators
Scripts & Functions Scripts and functions are contained in .m-files
McLAB: Compiler Tools for MATLAB
MATLAB Programs Chapter 6 Attaway MATLAB 4E.
Introduction to MATLAB
UNIT V Run Time Environments.
Corresponds with Chapter 5
SPL – PS1 Introduction to C++.
Presentation transcript:

McLab Tutorial Part 2 – Introduction to MATLAB Functions and Scripts Data and Variables Other Tricky "Features" 6/4/2011Matlab- 1McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen Lameed TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AA A AAAA A A A A A A

26/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen Lameed

Basic Structure of a MATLAB function 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 3 >> [a,b] = ProdSum([10,20,30],3) a = 6000 b = 60 >> ProdSum([10,20,30],2) ans = 200 >> ProdSum(‘abc’,3) ans = >> ProdSum([ ],3) ans =

Basic Structure of a MATLAB function (2) 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 4 >> [a,b] = a = b = >> [a,b] = a = 6 b = 6 >> magic(3) ans = >>ProdSum(ans,3) ans=96

Basic Structure of a MATLAB function (3) 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 5 >> ProdSum([10,20,30],'a') ??? For colon operator with char operands, first and last operands must be char. Error in ==> ProdSum at 4 for i = 1:n >> ProdSum([10,20,30],i) Warning: Colon operands must be real scalars. > In ProdSum at 4 ans = 1 >> ProdSum([10,20,30],[3,4,5]) ans = 6000

Primary, nested and sub-functions 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 6 Primary Function Nested Function Sub- Function

Basic Structure of a MATLAB script 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 7 >> clear >> a = [10, 20, 30]; >> n = 3; >> whos Name Size Bytes Class a 1x3 24 double n 1x1 8 double >> ProdSumScript() >> whos Name Size Bytes Class a 1x3 24 double i 1x1 8 double n 1x1 8 double prod 1x1 8 double sum 1x1 8 double

Directory Structure and Path Each directory can contain: –.m files (which can contain a script or functions) – a private/ directory – a package directory of the form +pkg/ – a type-specialized directory of the At run-time: – current directory (implicit 1 st element of path) – path of directories – both the current directory and path can be changed at runtime ( cd and setpath functions) 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 8

Function/Script Lookup Order (call in the body of a function f ) Nested function (in scope of f) Sub-function (in same file as f) Function in /private sub-directory of directory containing f. 1 st matching function, based on function name and type of first argument, looking in type- specialized directories, looking first in current directory and then along path. 1 st matching function/script, based on function name only, looking first in current directory and then along path. 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 9 function f... foo(a);... end

Function/Script Lookup Order (call in the body of a script s) Function in /private sub-directory of directory of last called function (not the /private sub-directory of the directory containing s). 1 st matching function/script, based on function name, looking first in current directory and then along path. 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 10 dir1/ dir2/ f.m s.m g.m h.m private/ private/ foo.m foo.m % in s.m... foo(a);...

Copy Semantics 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 11 >> m = [10, 20, 30] m = >> n = 2 * a n = >> CopyEx(m,n) ans = >> m = CopyEx(m,n) m =

126/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen Lameed

>> clear >> a = [10, 20, 30] a = >> b = int32(a) b = >> c = isinteger(b) c = 1 >> d = complex(int32(4),int32(3)) d = 4 + 3i 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 13 Examples of base types >> whos Name Size Bytes Class Attributes a 1x3 24 double b 1x3 12 int32 c 1x1 1 logical d 1x1 8 int32 complex >> isinteger(c) ans = 0 >> isnumeric(a) ans = 1 >> isnumeric(c) ans = 0 >> isreal(d) ans = 0

MATLAB base data types 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 14

Data Conversions double + double  double single + double  double double:complex + double  double:complex int32 + double  int32 logical + double  error, not allowed int16 + int32  error, not allowed int32:complex + int32:complex  error, not defined 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 15

MATLAB types: high-level 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 16

Cell array and struct example >> s = struct('name', 'Laurie', 'student', students) s = 1x3 struct array with fields: name student >> a = s(1) a = name: 'Laurie' student: 'Nurudeen' >> a.age = 21 a = name: 'Laurie' students: 'Nurudeen' age: 21 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 17 >> students = {'Nurudeen', 'Rahul', 'Jesse'} students = 'Nurudeen' 'Rahul' 'Jesse' >> cell = students(1) cell = 'Nurudeen' >> contents = students{1} contents =Nurudeen >> whos Name Size Bytes Class cell cell contents 1x8 16 char students 1x3 372 cell

Local variables Variables are not explicitly declared. Local variables are allocated in the current workspace. All input and output parameters are local. Local variables are allocated upon their first definition or via a load statement. – x =... – x(i) =... – load (’f.mat’, ’x’) Local variables can hold data with different types at different places in a function/script. 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 18

Global and Persistent Variables Variables can be declared to be global. – global x; Persistent declarations are allowed within function bodies only (not allowed in scripts or read-eval-print loop). – persistent y; A persistent or global declaration of x should cover all defs and uses of x in the body of the function/script. 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 19

Variable Workspaces There is a workspace for global and persistent variables. There is a workspace associated with the read- eval-print loop. Each function call creates a new workspace (stack frame). A script uses the workspace of its caller (either a function workspace or the read-eval-print workspace). 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 20

Variable Lookup If the variable has been declared global or persistent in the function body, look it up in the global/persistent workspace. Otherwise, lookup in the current workspace (either the read-eval-print workspace or the top-most function call workspace). For nested functions, use the standard scoping mechanisms. 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 21

Local/Global Example 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 22 >> clear >> global sum >> sum = 0; >> ProdSumGlobal([10,20,30],3) ans = 6000 >> sum sum = 60 >> whos Name Size Bytes Class Attributes ans 1x1 8 double sum 1x1 8 double global

236/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen Lameed

Looking up an identifier First lookup as a variable. If a variable not found, then look up as a function. When function/script first loaded, assign a "kind" to each identifier. VAR – only lookup as a variable, FN – only lookup as a function, ID – use the old style general lookup. 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 24 Old style general lookup - interpreter MATLAB 7 lookup - JIT

Kind Example 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 25 >> KindEx (3) x = i f r = i ans = VAR: r, a, x, f FN: i, j, sum, sin ID: s

Irritating Front-end "Features" keyword end not always required at the end of a function (often missing in files with only one function). command syntax – length('x') or length x – cd('mydirname') or cd mydirname arrays can be defined with or without commas: [10, 20, 30] or [ ] sometimes newlines have meaning: – a = [ ]; // defines a 2x3 matrix – a = [ ]; // defines a 1x6 matrix – a = [ ; ]; // defines a 2x3 matrix – a = [ ; ]; // defines a 2x3 matrix 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 26

“Evil” Dynamic Features not all input arguments required do not need to use all output arguments eval, evalin, assignin cd, addpath load 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedMatlab - 27