Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/29/2003.

Slides:



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

Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters.
COMP 116: Introduction to Scientific Programming Lecture 37: Final Review.
Introduction To MATLAB Programming
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
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 C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/27/2003.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
MATLAB Loops and Branching.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/22/2003.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/27/2003.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/22/2003.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
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) 11/03/2003.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Chapter 9 Above: An early computer input/output device on the IBM 7030 (STRETCH)
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.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
Introduction to File I/O High-Level Functions 1.Data files 2."High level" File I/O 3.dlmread() 4.xlsread() 1.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
Matlab Training Session 10: Loading Binary Data Course Website: Training Sessions.htm.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
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 &
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
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.
Digital Image Processing Introduction to M-function Programming.
Introduction to MATLAB Section2, statistics course Third year biomedical dept. Dina El Kholy, Ahmed Dalal.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()
General Computer Science for Engineers CISC 106 Lecture 15 Dr. John Cavazos Computer and Information Sciences 03/16/2009.
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
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.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes Contrasting MATLAB with C MATLABC language Workspace - interactive computation No real.
How to use MATLAB (using M-files) Double click this icon To start Matlab 6.5.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
Input Output IO and datafiles Credits also to Prof Selim Aksoy (Bilkent University.
Reading and Writing Data Files
Chapter 6 JavaScript: Introduction to Scripting
Introduction to MATLAB
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
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
MATLAB How to use (using M-files) Double click this icon
Matlab review Matlab is a numerical analysis system
MATLAB How to use (using M-files) Double click this icon
MATLAB How to use (using M-files)
Introduction to MATLAB
Introduction to MATLAB
Introduction to MATLAB
Introduction to MATLAB
Note on Indexing of Array Elements
Introduction to MATLAB
Introduction to MATLAB
Presentation transcript:

Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/29/2003

Overview for 10/30/2003 Brief review of topics covered in last session (10/29/2003) MATLAB Functions Looping! Optimization Review of topics covered thus far

Review for 10/29/2003 File Output MATLAB Scripts and Functions

High Level File Output Some of the input commands have corresponding high-level output commands csvwrite dlmwrite

csvwrite Write a matrix to a comma-seperated value file Syntax: –csvwrite('filename',M) –csvwrite('filename',M,row,col) Ex. csvwrite(‘blah.csv’,a);

dlmwrite Writes a matrix to a delimited file (using the delimiter you specify) Syntax: –dlmwrite(filename,M,delimiter) –dlmwrite(filename,M,delimiter,row,col) Ex. dlmwrite(‘blah.txt’,a,’:’);

Low-Level file I/O fopen fclose fprintf fgetl / fgets

fopen Opens a file and returns the handle to the file object File_ID = fopen(‘blah.txt’) Capturing the file handle is necessary to write or read to/from the file

fclose Closes a file associated with a specific file identification handle Ex. fclose(File_ID); Ex. fclose(‘all’);

fprintf Multi-use: can output to a file or a screen Ex. fprintf(fid,'%6.2f %12.8f\n',y); %6.2f means a floating point with 6 leading decimals and 2 trailing Specifying 1 instead of fid will output to the screen

fgetl / fgets Get line and get string, respectively. Fgetl will get you a line without the newline character at the end, while fgets will preserve the newline character (\n). Syntax: –Line = fgetl(File_ID); –Line = fgets(File_ID);

Programming in MATLAB Two types of files: –Scripts –Functions

MATLAB Scripts Scripts are MATLAB commands stored in text files. When you type the name of the script file at the MATLAB prompt the commands in the script file are executed as if you had typed them in from the keyboard. Scripts end with the extension.m Referred to as M-Files

MATLAB Functions Functions are similar to scripts Functions may take arguments Functions may return one or more values

MATLAB Functions, con’t: 2 function [output] = function_name(input_arguments) The above is a function header and should be the first non-comment line in the function file Comments may be placed above the function header

MATLAB Functions, con’t: 3 Example function function [output] = square(input) output = input*input; Body of functions can contain code just like scripts could

Looping! Scripts and functions also allow the ability to loop using conventional for and while loops. Note that the interpreter also lets you do it, it is simply less easy to grasp

For Loops Common to other programming languages for variable = expression statement... statement end

For Loops, con’t: 2 Example: (taken from MATLAB help) a = zeros(k,k) % Preallocate matrix for m = 1:k for n = 1:k a(m,n) = 1/(m+n -1); end

For Loops, con’t: 3 The looping variable is defined in much the same way that we defined arrays/vectors. Ex. m = 1:k Or m = 1:10

For Loops, con’t: 4 Loops are shown to end by the keyword “end” Curly braces are not present to subdivide packets of code Make use of adequate white-space and tabbing to improve code readability

While Loops Similar to while loops in other languages while expression statement … end

While Loops, con’t: 2 Ex. (taken from help while) while (1+eps) > 1 eps = eps/2; end

While Loops, con’t: 3 Same notes apply to while loops. Code is separated by the keyword “end”

Looping conclusion Some other aspects of looping exist Use help while and help for to see them

MATLAB Code Optimization Two ways to optimize MATLAB code Vectorize code Preallocate matrices Information contained here: p1/help/techdoc/matlab_prog/ch10_p47.shtml

Review Week 1: –MATLAB GUI –Simple Commands –Declaring Variables –Simple functions Week 2: –Plotting –File Input –File Output

MATLAB GUI Launch Pad / Toolbox Workspace Current Directory Command History Command Window

Simple Commands who whos save clear load

Declaring a variable in MATLAB Not necessary to specify a type. (Such as int or float) Several kinds of variables: –Vector –Matrix –Structure –Cell array

Declaring a variable, con’t: 2 For an integer or floating point number: simply set a variable name equal to some character Ex. A = 5; Or A = 5

Sample MATLAB functions Min Max Median Mean Sum Diff

Plotting Several types of plots available Plot Polar Bar Hist

Plot() con’t: 3 – Color options Color options: –Yellow - ‘y’ –Magenta - ‘m’ –Cyan - ‘c’ –Red - ‘r’ –Green - ‘g’ –Blue - ‘b’ –White - ‘w’ –Black - ‘k’ Example: plot(temp, ‘y’);

Plot() con’t: 4 – Line options Line styles: –- solid line (default) –-- dashed line –: dotted line –-. dash-dot line

High-Level File I/O I/O = input/output 3 important commands for input –csvread –dlmread –textread

Look Ahead Next week: Review of any topics requested Profiling your code for speed Each day will have a separate part of the project

End Happy Halloween!