SlideShare a Scribd company logo
Last modified:January 1, 1970 GMT
An Introduction to Matlab(tm): Lesson 5
USING m-FILES - SCRATCH and FUNCTION FILES
Sometimes it is convenient to write a number of lines of
'matlab' code before executing the commands. We have seen how this
can be accomplished using the line continuation ellipsis; however,
it was noted in this approach that any mistake in the code required
the entire string to be entered again. Using an m-file we can
write a number of lines of 'matlab' code and store it in a file
whose name we select with the suffix '.m' added. Subsequently the
command string or file content can be executed by invoking the name
of the file while in 'matlab'. This application is called a
SCRATCH FILE.
On occasion it is convenient to express a function in 'matlab'
rather than calculating or defining a particular matrix. The many
'matlab' stored functions such as sin(x) and log(x) are examples of
functions. We can write functions of our definition to evaluate
parameters of our particular interest. This application of m-files
is called FUNCTION FILES.
SCRATCH FILES
A scratch file must be prepared in a text editor in ASCII
format and stored in the directory from which you invoked the
command to download 'matlab'. The name can be any legitimate file
name with the '.m' suffix.
As an example, suppose we wish to prepare a x-y plot of the
function
y = e-x/10 sin(x)

0 x ø 10 .

To accomplish this using a scratch ".m-file", we will call the file
'explot.m'. Open the file in a text editor and type the code
below. (Note the use of the '%' in line 1 below to create a
comment. Any text or commands typed on a line after the'%' will be
treated as a comment and ignored in the executable code.)
% A scratch m-file to plot exp(-x/10)sin(x)
x = [ 0:.2:10 ];
y = exp(-x/10) .* sin(x);
plot(x,y),...
title('EXPONENTIAL DAMPED SINE FUNCTION'),...
xlabel('x'),...
ylabel('y'),...
text(.6,.7,'y = exp(-x/10)*sin(x)','sc')
Store this file under the name 'explot.m in your 'matlab'
directory. Now when you are in 'matlab', any time you type the
command 'explot' the x-y plot of the damped exponential sine
function will appear.
FUNCTION FILES
Suppose that you wish to have in your 'matlab' workspace a
function that calculates the sine of the angle when the argument is
in degrees. The matlab function sin(x) requires that x be in
radians. A function can be written that use the 'matlab' sin(x)
function, where x is in radians, to accomplish the objective of
calculating the sine of an argument expressed in degrees. Again,
as in the case of the scratch file, we must prepare the file in
ASCII format using a text editor, and we must add the suffix '.m'
to the file. HERE THE NAME OF THE FILE MUST BE THE NAME OF THE
FUNCTION.
The following code will accomplish our objective. Note that the
first line must always begin with the word "function" followed by
the name of the function expressed as " y = function-name". Here
the function name is "sind(x)". This is the name by which we will
call for the function once it is stored.
function y = sind(x)
% This function calculates the sine when the argument is degrees
% Note that array multiplication and division allows this to
% operate on scalars, vectors and matrices.
y = sin( x .* pi ./ 180 )
Again, note the rule in writing the function is to express in the
first line of the file the word 'function', followed by
y = function call,
here 'sind' with the function argument in parentheses, 'x' is the
function call. Thus,
function y=sind(x)
Now every time we type sind(x) in 'matlab', it will return the
value of the sine function calculated under the assumption that x
is in degrees. Of course any matrix or expression involving a
matrix can be written in the argument.
Back to Matlab In-House Tutorials

More Related Content

PPTX
Chapter iii(advance function)
PDF
Swift 3.0 の新しい機能(のうちの9つ)
PPTX
Multiple parameters
PPTX
Lecture 1 mte 407
PPTX
Lecture 1 mte 407
PPT
Lecture 7- Operators and Expressions
PPT
Java Script Introduction
PPTX
Strings
Chapter iii(advance function)
Swift 3.0 の新しい機能(のうちの9つ)
Multiple parameters
Lecture 1 mte 407
Lecture 1 mte 407
Lecture 7- Operators and Expressions
Java Script Introduction
Strings

What's hot (20)

PPT
Code rule
PPTX
Pointer to function 2
PPTX
Array, string and pointer
PDF
03 function overloading
PPT
C# Method overloading
PPTX
Presention programming
PDF
The Functional Programming Triad of fold, scan and iterate
PPTX
Fifth session
PDF
Functional Programming with Javascript
PPTX
C Programming Language Part 6
PDF
Implode & Explode in PHP
PPTX
Pointer to function 1
PPTX
R: Apply Functions
ODP
Functional Programming With Scala
PPTX
Functions (Computer programming and utilization)
PPTX
C Programming Language Part 8
PPTX
Javascript function
PPTX
PPTX
function, storage class and array and strings
Code rule
Pointer to function 2
Array, string and pointer
03 function overloading
C# Method overloading
Presention programming
The Functional Programming Triad of fold, scan and iterate
Fifth session
Functional Programming with Javascript
C Programming Language Part 6
Implode & Explode in PHP
Pointer to function 1
R: Apply Functions
Functional Programming With Scala
Functions (Computer programming and utilization)
C Programming Language Part 8
Javascript function
function, storage class and array and strings
Ad

Viewers also liked (20)

PDF
Grounding of Multi Cable Transits for on-shore use
PPT
Hitachi set free features Compressors system Designing 919825024651
PPT
1--GMT-PRES ENGLISH-MSTR
PPTX
Wireless Power Transmission Using Solar Power System
PPT
Wireless power transmission soubel
PPTX
Wireless power transmission and reception using solar power satellites and ...
DOCX
MICROWAVE POWER TRANSMISSIONS
DOC
[3] rpp ski vii 1 & 2
PDF
Base Transceiver Station Equipment
ODP
Wireless power Transfer by Microwave
PDF
Opti x rtn 910950980 hardware description wind
PPTX
Wireless power transmission via solar power satellite
PPT
02 opti x rtn 900 v100r002 system hardware-20100223-a
PPTX
Base Transceiver Station
PDF
RPP MTs Akidah Ahlak Kelas VII
PPT
telecommunication Towers
PDF
Ericsson BTS commisioning
PPT
01 Bts3900 System Structure
PPT
Bts installation & commisioning.ppt
PPT
Bts-site-installation
Grounding of Multi Cable Transits for on-shore use
Hitachi set free features Compressors system Designing 919825024651
1--GMT-PRES ENGLISH-MSTR
Wireless Power Transmission Using Solar Power System
Wireless power transmission soubel
Wireless power transmission and reception using solar power satellites and ...
MICROWAVE POWER TRANSMISSIONS
[3] rpp ski vii 1 & 2
Base Transceiver Station Equipment
Wireless power Transfer by Microwave
Opti x rtn 910950980 hardware description wind
Wireless power transmission via solar power satellite
02 opti x rtn 900 v100r002 system hardware-20100223-a
Base Transceiver Station
RPP MTs Akidah Ahlak Kelas VII
telecommunication Towers
Ericsson BTS commisioning
01 Bts3900 System Structure
Bts installation & commisioning.ppt
Bts-site-installation
Ad

Similar to Lesson 5 (20)

PDF
Matlab functions
PDF
Introduction to matlab
PDF
Introduction to matlab
PPT
MatlabIntro1234.ppt.....................
PPT
WIDI ediot autis dongok part 1.ediot lu lemot lu setan lu
PPT
WIDI FREAK MANUSIA SETENGAH EDIOTDAN LEMBOT
PPT
MatlabIntro.ppt
PPT
MatlabIntro.ppt
PPT
MatlabIntro.ppt
PPT
MatlabIntro.ppt
PPT
Matlab intro
PDF
Dsp lab _eec-652__vi_sem_18012013
PDF
Dsp lab _eec-652__vi_sem_18012013
PPT
Tips and Tricks using the Preprocessor
PPT
WIDI ediot autis dongok part 3.EDIOT LU LEMBOT LY
PPT
WIDI ediot autis dongok part 2.ediot lu lembot lu
PPTX
Introduction to matlab
PDF
User defined functions in matlab
DOCX
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
Matlab functions
Introduction to matlab
Introduction to matlab
MatlabIntro1234.ppt.....................
WIDI ediot autis dongok part 1.ediot lu lemot lu setan lu
WIDI FREAK MANUSIA SETENGAH EDIOTDAN LEMBOT
MatlabIntro.ppt
MatlabIntro.ppt
MatlabIntro.ppt
MatlabIntro.ppt
Matlab intro
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
Tips and Tricks using the Preprocessor
WIDI ediot autis dongok part 3.EDIOT LU LEMBOT LY
WIDI ediot autis dongok part 2.ediot lu lembot lu
Introduction to matlab
User defined functions in matlab
CS 23001 Computer Science II Data Structures & AbstractionPro.docx

More from Vinnu Vinay (11)

DOC
Matlab tut3
DOC
Matlab tut2
DOC
Matlab summary
DOC
Lesson 8
DOC
Lesson 7
DOC
Lesson 6
DOC
Lesson 4
DOC
Lesson 3
DOC
Lesson 2
DOC
matlab Lesson 1
DOC
Matlabtut1
Matlab tut3
Matlab tut2
Matlab summary
Lesson 8
Lesson 7
Lesson 6
Lesson 4
Lesson 3
Lesson 2
matlab Lesson 1
Matlabtut1

Recently uploaded (20)

PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Getting started with AI Agents and Multi-Agent Systems
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PPTX
Modernising the Digital Integration Hub
PDF
A novel scalable deep ensemble learning framework for big data classification...
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
Architecture types and enterprise applications.pdf
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
Enhancing emotion recognition model for a student engagement use case through...
PPTX
Web Crawler for Trend Tracking Gen Z Insights.pptx
DOCX
search engine optimization ppt fir known well about this
PPTX
O2C Customer Invoices to Receipt V15A.pptx
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
STKI Israel Market Study 2025 version august
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
DP Operators-handbook-extract for the Mautical Institute
Group 1 Presentation -Planning and Decision Making .pptx
Getting started with AI Agents and Multi-Agent Systems
Module 1.ppt Iot fundamentals and Architecture
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Final SEM Unit 1 for mit wpu at pune .pptx
Modernising the Digital Integration Hub
A novel scalable deep ensemble learning framework for big data classification...
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
Architecture types and enterprise applications.pdf
sustainability-14-14877-v2.pddhzftheheeeee
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Enhancing emotion recognition model for a student engagement use case through...
Web Crawler for Trend Tracking Gen Z Insights.pptx
search engine optimization ppt fir known well about this
O2C Customer Invoices to Receipt V15A.pptx
Zenith AI: Advanced Artificial Intelligence
STKI Israel Market Study 2025 version august
Assigned Numbers - 2025 - Bluetooth® Document
DP Operators-handbook-extract for the Mautical Institute

Lesson 5

  • 1. Last modified:January 1, 1970 GMT An Introduction to Matlab(tm): Lesson 5 USING m-FILES - SCRATCH and FUNCTION FILES Sometimes it is convenient to write a number of lines of 'matlab' code before executing the commands. We have seen how this can be accomplished using the line continuation ellipsis; however, it was noted in this approach that any mistake in the code required the entire string to be entered again. Using an m-file we can write a number of lines of 'matlab' code and store it in a file whose name we select with the suffix '.m' added. Subsequently the command string or file content can be executed by invoking the name of the file while in 'matlab'. This application is called a SCRATCH FILE. On occasion it is convenient to express a function in 'matlab' rather than calculating or defining a particular matrix. The many 'matlab' stored functions such as sin(x) and log(x) are examples of functions. We can write functions of our definition to evaluate parameters of our particular interest. This application of m-files is called FUNCTION FILES. SCRATCH FILES A scratch file must be prepared in a text editor in ASCII format and stored in the directory from which you invoked the command to download 'matlab'. The name can be any legitimate file name with the '.m' suffix. As an example, suppose we wish to prepare a x-y plot of the function y = e-x/10 sin(x) 0 x ø 10 . To accomplish this using a scratch ".m-file", we will call the file 'explot.m'. Open the file in a text editor and type the code below. (Note the use of the '%' in line 1 below to create a comment. Any text or commands typed on a line after the'%' will be treated as a comment and ignored in the executable code.) % A scratch m-file to plot exp(-x/10)sin(x) x = [ 0:.2:10 ]; y = exp(-x/10) .* sin(x);
  • 2. plot(x,y),... title('EXPONENTIAL DAMPED SINE FUNCTION'),... xlabel('x'),... ylabel('y'),... text(.6,.7,'y = exp(-x/10)*sin(x)','sc') Store this file under the name 'explot.m in your 'matlab' directory. Now when you are in 'matlab', any time you type the command 'explot' the x-y plot of the damped exponential sine function will appear. FUNCTION FILES Suppose that you wish to have in your 'matlab' workspace a function that calculates the sine of the angle when the argument is in degrees. The matlab function sin(x) requires that x be in radians. A function can be written that use the 'matlab' sin(x) function, where x is in radians, to accomplish the objective of calculating the sine of an argument expressed in degrees. Again, as in the case of the scratch file, we must prepare the file in ASCII format using a text editor, and we must add the suffix '.m' to the file. HERE THE NAME OF THE FILE MUST BE THE NAME OF THE FUNCTION. The following code will accomplish our objective. Note that the first line must always begin with the word "function" followed by the name of the function expressed as " y = function-name". Here the function name is "sind(x)". This is the name by which we will call for the function once it is stored. function y = sind(x) % This function calculates the sine when the argument is degrees % Note that array multiplication and division allows this to % operate on scalars, vectors and matrices. y = sin( x .* pi ./ 180 ) Again, note the rule in writing the function is to express in the first line of the file the word 'function', followed by y = function call, here 'sind' with the function argument in parentheses, 'x' is the function call. Thus, function y=sind(x) Now every time we type sind(x) in 'matlab', it will return the
  • 3. value of the sine function calculated under the assumption that x is in degrees. Of course any matrix or expression involving a matrix can be written in the argument. Back to Matlab In-House Tutorials