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.

Slides:



Advertisements
Similar presentations
Introducing JavaScript
Advertisements

Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
The Web Warrior Guide to Web Design Technologies
Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Programming with MATLAB
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
EGR 106 – Truss Design Project (cont.) Truss design programs Graphical interface tools in Matlab Saving and loading data Formatted output Project Assignment.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Dr. Jie Zou PHY Introduction to MATLAB Programming with MATLAB 1 1 Applied Numerical Methods with MATLAB for Engineers and Scientists, 2nd ed., Steven.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
AN ENGINEER’S GUIDE TO MATLAB
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 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 4.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (3): MATLAB Environment (Chapter 1)
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.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
1 JavaScript in Context. Server-Side Programming.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Input, Output, and Processing
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 Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Scientific Computing Introduction to Matlab Programming.
Part 1 Chapter 3 Programming with MATLAB PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and Prof. Steve Chapra, Tufts University.
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 &
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Asking the USER for values to use in a software 1 Input.
Part 1 Chapter 3 Programming with MATLAB PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and Prof. Steve Chapra, Tufts University.
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
GENERATION OF RANDOM NUMBERS
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.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
ES 240: Scientific and Engineering Computation. Chapter 1 Chapter 1: Mathematical Modeling, Numerical Methods and Problem Solving Uchechukwu Ofoegbu Temple.
Part 1 Chapter 1 Mathematical Modeling, Numerical Methods, and Problem Solving PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Matlab Data types, input and output. Data types Char: >> a = ‘ Jim ’ Char: >> a = ‘ Jim ’ Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64.
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
1 JavaScript in Context. Server-Side Programming.
Introduction to MATLAB Section2, statistics course Third year biomedical dept. Dina El Kholy, Ahmed Dalal.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Part 1 Chapter 1 Mathematical Modeling, Numerical Methods, and Problem Solving PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and.
Overview Input Input statements Menu statements Output Display statements fprintf statements Message boxes.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
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.
Introduction to Matlab PHY September Why Matlab? No need to compile code Huge amount of built-in functions Highly optimized and fast, in general.
EEE 161 Applied Electromagnetics
Programming with MATLAB
Basic operations in Matlab
script and function files
script and function files
Matlab.
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
244-2: MATLAB/Simulink FUNDAMENTALS
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

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 so named because the files are stored with a.m extension. There are two main kinds of M-file –Script files –Function files

Script Files A script file is merely a set of MATLAB commands that are saved on a file - when MATLAB runs a script file, it is as if you typed the characters stored in the file on the command window. Scripts can be executed either by typing their name (without the.m) in the command window, by selecting the Debug, Run (or Save and Run) command in the editing window, or by hitting the F5 key while in the editing window. Note that the latter two options will save any edits you have made, while the former will run the file as it exists on the drive.

Function Files Function files serve an entirely different purpose from script files. Function files can accept input arguments from and return outputs to the command window, but variables created and manipulated within the function do not impact the command window.

Function File Syntax The general syntax for a function is: function outvar = funcname(arglist) % helpcomments statements outvar = value; where –outvar: output variable name –funcname: function’s name –arglist: input argument list - comma-delimited list of what the function calls values passed to it –helpcomments: text to show with help funcname –statements: MATLAB commands for the function

Subfunctions A function file can contain a single function, but it can also contain a primary function and one or more subfunctions The primary function is whatever function is listed first in the M-file - its function name should be the same as the file name. Subfunctions are listed below the primary function. Note that they are only accessible by the main function and subfunctions within the same M-file and not by the command window or any other functions or scripts.

Input The easiest way to get a value from the user is the input command: –n = input('promptstring') MATLAB will display the characters in promptstring, and whatever value is typed is stored in n. For example, if you type pi, n will store … –n = input('promptstring', 's') MATLAB will display the characters in promptstring, and whatever characters are typed will be stored as a string in n. For example, if you type pi, n will store the letters p and i in a 2x1 char array.

Output The easiest way to display the value of a matrix is to type its name, but that will not work in function or script files. Instead, use the disp command disp(value) will show the value on the screen, and if it is a string, will enclose it in single quotes.

Formatted Output For formatted output, or for output generated by combining variable values with literal text, use the fprintf command: fprintf('format', x, y, …) where format is a string specifying how you want the value of the variables x, y, and more to be displayed - including literal text to be printed along with the values. The values in the variables are formatted based on format codes.

Format and Control Codes Within the format string, the following format codes define how a numerical value is displayed: %d - integer format %e - scientific format with lowercase e %E - scientific format with uppercase E %f - decidmal format %g - the more compact of %e or %f The following control codes produce special results within the format string: \n - start a new line \t - tab \\ - print the \ character To print a ' put two a pair of ' in the format string

Creating and Accessing Files MATLAB has a built-in file format that may be used to save and load the values in variables. save filename var1 var2 … varn saves the listed variables into a file named filename.mat. If no variable is listed, all variables are saved. load filename var1 var2 … varn loads the listed variables from a file named filename.mat. If no variable is listed, all variables in the file are loaded. Note - these are not text files!

Model Function Example Assuming a bungee jumper is in mid- flight, an analytical model for the jumper’s velocity, accounting for drag, is Dependent variable - velocity v Independent variables - time t Parameters - mass m, drag coefficient c d, gravitational acceleration g

Model Results Using a computer (or a calculator), the model can be used to generate a graphical representation of the system. For example, the graph below represents the velocity of a 68.1 kg jumper, assuming a drag coefficient of 0.25 kg/m