SlideShare a Scribd company logo
1 of 3
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

What's hot (20)

Code rule
Code ruleCode rule
Code rule
 
Pointer to function 2
Pointer to function 2Pointer to function 2
Pointer to function 2
 
Array, string and pointer
Array, string and pointerArray, string and pointer
Array, string and pointer
 
03 function overloading
03 function overloading03 function overloading
03 function overloading
 
C# Method overloading
C# Method overloadingC# Method overloading
C# Method overloading
 
Presention programming
Presention programmingPresention programming
Presention programming
 
The Functional Programming Triad of fold, scan and iterate
The Functional Programming Triad of fold, scan and iterateThe Functional Programming Triad of fold, scan and iterate
The Functional Programming Triad of fold, scan and iterate
 
Fifth session
Fifth sessionFifth session
Fifth session
 
Functional Programming with Javascript
Functional Programming with JavascriptFunctional Programming with Javascript
Functional Programming with Javascript
 
C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6
 
Implode & Explode in PHP
Implode & Explode in PHPImplode & Explode in PHP
Implode & Explode in PHP
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
R: Apply Functions
R: Apply FunctionsR: Apply Functions
R: Apply Functions
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
Functions (Computer programming and utilization)
Functions (Computer programming and utilization)Functions (Computer programming and utilization)
Functions (Computer programming and utilization)
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
Csharp generics
Csharp genericsCsharp generics
Csharp generics
 
Javascript function
Javascript functionJavascript function
Javascript function
 
Pointers
 Pointers Pointers
Pointers
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and strings
 

Viewers also liked

Grounding of Multi Cable Transits for on-shore use
Grounding of Multi Cable Transits for on-shore useGrounding of Multi Cable Transits for on-shore use
Grounding of Multi Cable Transits for on-shore useMathieu Melenhorst
 
Hitachi set free features Compressors system Designing 919825024651
Hitachi set free features Compressors system Designing 919825024651Hitachi set free features Compressors system Designing 919825024651
Hitachi set free features Compressors system Designing 919825024651Sdsplit Split
 
Wireless Power Transmission Using Solar Power System
Wireless Power Transmission Using Solar Power System Wireless Power Transmission Using Solar Power System
Wireless Power Transmission Using Solar Power System elprocus
 
Wireless power transmission soubel
Wireless power transmission   soubelWireless power transmission   soubel
Wireless power transmission soubel98422
 
Wireless power transmission and reception using solar power satellites and ...
Wireless  power  transmission and reception using solar power satellites and ...Wireless  power  transmission and reception using solar power satellites and ...
Wireless power transmission and reception using solar power satellites and ...PRADEEP Cheekatla
 
MICROWAVE POWER TRANSMISSIONS
MICROWAVE POWER TRANSMISSIONSMICROWAVE POWER TRANSMISSIONS
MICROWAVE POWER TRANSMISSIONSSubhankar Ghosh
 
[3] rpp ski vii 1 & 2
[3] rpp ski vii 1 & 2[3] rpp ski vii 1 & 2
[3] rpp ski vii 1 & 2selikurfa
 
Base Transceiver Station Equipment
Base Transceiver Station EquipmentBase Transceiver Station Equipment
Base Transceiver Station EquipmentArash Soltani
 
Wireless power Transfer by Microwave
Wireless power Transfer by MicrowaveWireless power Transfer by Microwave
Wireless power Transfer by MicrowaveBhushan Kumbhalkar
 
Opti x rtn 910950980 hardware description wind
Opti x rtn 910950980 hardware description windOpti x rtn 910950980 hardware description wind
Opti x rtn 910950980 hardware description windnctgayaranga
 
Wireless power transmission via solar power satellite
Wireless power transmission via solar power satelliteWireless power transmission via solar power satellite
Wireless power transmission via solar power satelliteFaizy Ali
 
02 opti x rtn 900 v100r002 system hardware-20100223-a
02 opti x rtn 900 v100r002 system hardware-20100223-a02 opti x rtn 900 v100r002 system hardware-20100223-a
02 opti x rtn 900 v100r002 system hardware-20100223-aWaheed Ali
 
Base Transceiver Station
Base Transceiver StationBase Transceiver Station
Base Transceiver StationShubham Singhal
 
RPP MTs Akidah Ahlak Kelas VII
RPP MTs Akidah Ahlak Kelas VIIRPP MTs Akidah Ahlak Kelas VII
RPP MTs Akidah Ahlak Kelas VIIDiva Pendidikan
 
telecommunication Towers
telecommunication Towerstelecommunication Towers
telecommunication TowersTowerpeople
 
Ericsson BTS commisioning
Ericsson BTS commisioningEricsson BTS commisioning
Ericsson BTS commisioningShahid Rasool
 
01 Bts3900 System Structure
01 Bts3900 System Structure01 Bts3900 System Structure
01 Bts3900 System StructureAtif Mahmood
 
Bts installation & commisioning.ppt
Bts installation & commisioning.pptBts installation & commisioning.ppt
Bts installation & commisioning.pptAIRTEL
 
Bts-site-installation
 Bts-site-installation Bts-site-installation
Bts-site-installationAli Usman
 

Viewers also liked (20)

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

Similar to Lesson 5

Similar to Lesson 5 (20)

Matlab functions
Matlab functionsMatlab functions
Matlab functions
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
Matlab intro
Matlab introMatlab intro
Matlab intro
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
Tips and Tricks using the Preprocessor
Tips and Tricks using the Preprocessor Tips and Tricks using the Preprocessor
Tips and Tricks using the Preprocessor
 
Matlab Manual
Matlab ManualMatlab Manual
Matlab Manual
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
User defined functions in matlab
User defined functions in  matlabUser defined functions in  matlab
User defined functions in matlab
 
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docxCS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
 
Matlab: Procedures And Functions
Matlab: Procedures And FunctionsMatlab: Procedures And Functions
Matlab: Procedures And Functions
 
Procedures And Functions in Matlab
Procedures And Functions in MatlabProcedures And Functions in Matlab
Procedures And Functions in Matlab
 
Functions & Closures in Scala
Functions & Closures in ScalaFunctions & Closures in Scala
Functions & Closures in Scala
 
Functions & Closures in Scala
Functions & Closures in ScalaFunctions & Closures in Scala
Functions & Closures in Scala
 
Functions & closures
Functions & closuresFunctions & closures
Functions & closures
 

More from Vinnu Vinay (11)

Matlab tut3
Matlab tut3Matlab tut3
Matlab tut3
 
Matlab tut2
Matlab tut2Matlab tut2
Matlab tut2
 
Matlab summary
Matlab summaryMatlab summary
Matlab summary
 
Lesson 8
Lesson 8Lesson 8
Lesson 8
 
Lesson 7
Lesson 7Lesson 7
Lesson 7
 
Lesson 6
Lesson 6Lesson 6
Lesson 6
 
Lesson 4
Lesson 4Lesson 4
Lesson 4
 
Lesson 3
Lesson 3Lesson 3
Lesson 3
 
Lesson 2
Lesson 2Lesson 2
Lesson 2
 
matlab Lesson 1
matlab Lesson 1matlab Lesson 1
matlab Lesson 1
 
Matlabtut1
Matlabtut1Matlabtut1
Matlabtut1
 

Recently uploaded

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 

Recently uploaded (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 

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