SlideShare a Scribd company logo
1 of 2
Download to read offline
http://www.tutorialspoint.com/matlab/matlab_m_files.htm Copyright © tutorialspoint.com
MATLAB M-FILES
So far, we have used MATLAB environment as a calculator. However, MATLAB is also a powerfulprogramming
language, as wellas aninteractive computationalenvironment.
Inprevious chapters, youhave learned how to enter commands fromthe MATLAB command prompt. MATLAB
also allows youto write series of commands into a file and execute the file as complete unit, like writing a function
and calling it.
The M Files
MATLAB allows writing two kinds of programfiles:
Scripts - script files are programfiles with.m extension. Inthese files, youwrite series of commands,
whichyouwant to execute together. Scripts do not accept inputs and do not returnany outputs. They
operate ondata inthe workspace.
Functions - functions files are also programfiles with.m extension. Functions canaccept inputs and
returnoutputs. Internalvariables are localto the function.
Youcanuse the MATLAB Editor or any other text editor to create your .mfiles. Inthis section, we willdiscuss
the script files. A script file contains multiple sequentiallines of MATLAB commands and functioncalls. Youcan
runa script by typing its name at the command line.
Creating and Running Script File
To create scripts files, youneed to use a text editor. Youcanopenthe MATLAB editor intwo ways:
Using the command prompt
Using the IDE
If youare using the command prompt, type edit inthe command prompt. This willopenthe editor. Youcan
directly type edit and thenthe filename (with.mextension)
edit
Or
edit <filename>
The above command willcreate the file indefault MATLAB directory. If youwant to store allprogramfiles ina
specific folder, thenyouwillhave to provide the entire path.
Let us create a folder named progs. Type the following commands at the command prompt(>>):
mkdir progs % create directory progs under default directory
chdir progs % changing the current directory to progs
edit prog1.m % creating an m file named prog1.m
If youare creating the file for first time, MATLAB prompts youto confirmit. Click Yes.
Alternatively, if youare using the IDE, choose NEW -> Script. This also opens the editor and creates a file
named Untitled. Youcanname and save the file after typing the code.
Type the following code inthe editor:
NoOfStudents = 6000;
TeachingStaff = 150;
NonTeachingStaff = 20;
Total = NoOfStudents + TeachingStaff ...
+ NonTeachingStaff;
disp(Total);
After creating and saving the file, youcanrunit intwo ways:
Clicking the Run buttononthe editor window or
Just typing the filename (without extension) inthe command prompt: >> prog1
The command window prompt displays the result:
6170
Example
Create a script file, and type the following code:
a = 5; b = 7;
c = a + b
d = c + sin(b)
e = 5 * d
f = exp(-d)
Whenthe above code is compiled and executed, it produces the following result:
c =
12
d =
12.6570
e =
63.2849
f =
3.1852e-06

More Related Content

What's hot (8)

postgres loader
postgres loaderpostgres loader
postgres loader
 
M5 l7-rails controllers-response-handout
M5 l7-rails controllers-response-handoutM5 l7-rails controllers-response-handout
M5 l7-rails controllers-response-handout
 
cpp-file-handling
cpp-file-handlingcpp-file-handling
cpp-file-handling
 
Pluggable Pipelines
Pluggable PipelinesPluggable Pipelines
Pluggable Pipelines
 
M batching
M batchingM batching
M batching
 
Mule batch
Mule batchMule batch
Mule batch
 
Mule batch processing
Mule batch processingMule batch processing
Mule batch processing
 
Batch job processing
Batch job processingBatch job processing
Batch job processing
 

Viewers also liked

Viewers also liked (14)

Matlab Text Files
Matlab Text FilesMatlab Text Files
Matlab Text Files
 
Matlab: Procedures And Functions
Matlab: Procedures And FunctionsMatlab: Procedures And Functions
Matlab: Procedures And Functions
 
147994600 transformada-de-fourier
147994600 transformada-de-fourier147994600 transformada-de-fourier
147994600 transformada-de-fourier
 
matab no2
matab no2matab no2
matab no2
 
Matlab m files and scripts
Matlab m files and scriptsMatlab m files and scripts
Matlab m files and scripts
 
Driving Greater CRM Productivity with M-Files Solution for Law Firms
Driving Greater CRM Productivity with M-Files Solution for Law FirmsDriving Greater CRM Productivity with M-Files Solution for Law Firms
Driving Greater CRM Productivity with M-Files Solution for Law Firms
 
Algoritma Rinaldi Munir
Algoritma Rinaldi MunirAlgoritma Rinaldi Munir
Algoritma Rinaldi Munir
 
Mk ed4 ekf
Mk ed4 ekfMk ed4 ekf
Mk ed4 ekf
 
[EN] ECM: Whithersoever | Keynote by Ulrich Kampffmeyer @ M-Files EMEA Partne...
[EN] ECM: Whithersoever | Keynote by Ulrich Kampffmeyer @ M-Files EMEA Partne...[EN] ECM: Whithersoever | Keynote by Ulrich Kampffmeyer @ M-Files EMEA Partne...
[EN] ECM: Whithersoever | Keynote by Ulrich Kampffmeyer @ M-Files EMEA Partne...
 
Matlab Basic Tutorial
Matlab Basic TutorialMatlab Basic Tutorial
Matlab Basic Tutorial
 
Simulating communication systems with MATLAB: An introduction
Simulating communication systems with MATLAB: An introductionSimulating communication systems with MATLAB: An introduction
Simulating communication systems with MATLAB: An introduction
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to Matlab
 
Matlab Introduction
Matlab IntroductionMatlab Introduction
Matlab Introduction
 
Simulation of Wireless Communication Systems
Simulation of Wireless Communication SystemsSimulation of Wireless Communication Systems
Simulation of Wireless Communication Systems
 

Similar to Matlab m files

help with Linux/Unix starting
help with Linux/Unix startinghelp with Linux/Unix starting
help with Linux/Unix starting
dummy
 
Lab #9 and 10 Web Server ProgrammingCreate a New Folder I s.docx
Lab #9 and 10 Web Server ProgrammingCreate a New Folder  I s.docxLab #9 and 10 Web Server ProgrammingCreate a New Folder  I s.docx
Lab #9 and 10 Web Server ProgrammingCreate a New Folder I s.docx
DIPESH30
 
Vipul divyanshu mahout_documentation
Vipul divyanshu mahout_documentationVipul divyanshu mahout_documentation
Vipul divyanshu mahout_documentation
Vipul Divyanshu
 
Unix Lec2
Unix Lec2Unix Lec2
Unix Lec2
Dr.Ravi
 
fileop report
fileop reportfileop report
fileop report
Jason Lu
 

Similar to Matlab m files (20)

Introduction to Matlab Scripts
Introduction to Matlab ScriptsIntroduction to Matlab Scripts
Introduction to Matlab Scripts
 
Mat-lab image processing tatorial
Mat-lab  image processing tatorialMat-lab  image processing tatorial
Mat-lab image processing tatorial
 
CSO Laboratory Manual
CSO Laboratory ManualCSO Laboratory Manual
CSO Laboratory Manual
 
help with Linux/Unix starting
help with Linux/Unix startinghelp with Linux/Unix starting
help with Linux/Unix starting
 
Introduction to MATLAB 2
Introduction to MATLAB 2Introduction to MATLAB 2
Introduction to MATLAB 2
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
File handling
File handlingFile handling
File handling
 
Install
InstallInstall
Install
 
Matlab Introduction
Matlab IntroductionMatlab Introduction
Matlab Introduction
 
MATLAB Program Example Research Help
MATLAB Program Example Research HelpMATLAB Program Example Research Help
MATLAB Program Example Research Help
 
Matlab - Introduction and Basics
Matlab - Introduction and BasicsMatlab - Introduction and Basics
Matlab - Introduction and Basics
 
Lab #9 and 10 Web Server ProgrammingCreate a New Folder I s.docx
Lab #9 and 10 Web Server ProgrammingCreate a New Folder  I s.docxLab #9 and 10 Web Server ProgrammingCreate a New Folder  I s.docx
Lab #9 and 10 Web Server ProgrammingCreate a New Folder I s.docx
 
A05
A05A05
A05
 
Mbuild help
Mbuild helpMbuild help
Mbuild help
 
Dumping and Cracking SAM Hashes to Extract Plaintext Passwords
Dumping and Cracking SAM Hashes to Extract Plaintext PasswordsDumping and Cracking SAM Hashes to Extract Plaintext Passwords
Dumping and Cracking SAM Hashes to Extract Plaintext Passwords
 
Vipul divyanshu mahout_documentation
Vipul divyanshu mahout_documentationVipul divyanshu mahout_documentation
Vipul divyanshu mahout_documentation
 
Unix Lec2
Unix Lec2Unix Lec2
Unix Lec2
 
Linux intro 5 extra: makefiles
Linux intro 5 extra: makefilesLinux intro 5 extra: makefiles
Linux intro 5 extra: makefiles
 
Lab 1 Essay
Lab 1 EssayLab 1 Essay
Lab 1 Essay
 
fileop report
fileop reportfileop report
fileop report
 

More from pramodkumar1804 (20)

Matlab syntax
Matlab syntaxMatlab syntax
Matlab syntax
 
Matlab strings
Matlab stringsMatlab strings
Matlab strings
 
Matlab simulink
Matlab simulinkMatlab simulink
Matlab simulink
 
Matlab polynomials
Matlab polynomialsMatlab polynomials
Matlab polynomials
 
Matlab plotting
Matlab plottingMatlab plotting
Matlab plotting
 
Matlab overview 3
Matlab overview 3Matlab overview 3
Matlab overview 3
 
Matlab overview 2
Matlab overview 2Matlab overview 2
Matlab overview 2
 
Matlab overview
Matlab overviewMatlab overview
Matlab overview
 
Matlab operators
Matlab operatorsMatlab operators
Matlab operators
 
Matlab variables
Matlab variablesMatlab variables
Matlab variables
 
Matlab numbers
Matlab numbersMatlab numbers
Matlab numbers
 
Matlab matrics
Matlab matricsMatlab matrics
Matlab matrics
 
Matlab loops 2
Matlab loops 2Matlab loops 2
Matlab loops 2
 
Matlab loops
Matlab loopsMatlab loops
Matlab loops
 
Matlab integration
Matlab integrationMatlab integration
Matlab integration
 
Matlab graphics
Matlab graphicsMatlab graphics
Matlab graphics
 
Matlab gnu octave
Matlab gnu octaveMatlab gnu octave
Matlab gnu octave
 
Matlab operators
Matlab operatorsMatlab operators
Matlab operators
 
Matlab functions
Matlab functionsMatlab functions
Matlab functions
 
Matlab differential
Matlab differentialMatlab differential
Matlab differential
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 

Matlab m files

  • 1. http://www.tutorialspoint.com/matlab/matlab_m_files.htm Copyright © tutorialspoint.com MATLAB M-FILES So far, we have used MATLAB environment as a calculator. However, MATLAB is also a powerfulprogramming language, as wellas aninteractive computationalenvironment. Inprevious chapters, youhave learned how to enter commands fromthe MATLAB command prompt. MATLAB also allows youto write series of commands into a file and execute the file as complete unit, like writing a function and calling it. The M Files MATLAB allows writing two kinds of programfiles: Scripts - script files are programfiles with.m extension. Inthese files, youwrite series of commands, whichyouwant to execute together. Scripts do not accept inputs and do not returnany outputs. They operate ondata inthe workspace. Functions - functions files are also programfiles with.m extension. Functions canaccept inputs and returnoutputs. Internalvariables are localto the function. Youcanuse the MATLAB Editor or any other text editor to create your .mfiles. Inthis section, we willdiscuss the script files. A script file contains multiple sequentiallines of MATLAB commands and functioncalls. Youcan runa script by typing its name at the command line. Creating and Running Script File To create scripts files, youneed to use a text editor. Youcanopenthe MATLAB editor intwo ways: Using the command prompt Using the IDE If youare using the command prompt, type edit inthe command prompt. This willopenthe editor. Youcan directly type edit and thenthe filename (with.mextension) edit Or edit <filename> The above command willcreate the file indefault MATLAB directory. If youwant to store allprogramfiles ina specific folder, thenyouwillhave to provide the entire path. Let us create a folder named progs. Type the following commands at the command prompt(>>): mkdir progs % create directory progs under default directory chdir progs % changing the current directory to progs edit prog1.m % creating an m file named prog1.m If youare creating the file for first time, MATLAB prompts youto confirmit. Click Yes.
  • 2. Alternatively, if youare using the IDE, choose NEW -> Script. This also opens the editor and creates a file named Untitled. Youcanname and save the file after typing the code. Type the following code inthe editor: NoOfStudents = 6000; TeachingStaff = 150; NonTeachingStaff = 20; Total = NoOfStudents + TeachingStaff ... + NonTeachingStaff; disp(Total); After creating and saving the file, youcanrunit intwo ways: Clicking the Run buttononthe editor window or Just typing the filename (without extension) inthe command prompt: >> prog1 The command window prompt displays the result: 6170 Example Create a script file, and type the following code: a = 5; b = 7; c = a + b d = c + sin(b) e = 5 * d f = exp(-d) Whenthe above code is compiled and executed, it produces the following result: c = 12 d = 12.6570 e = 63.2849 f = 3.1852e-06