際際滷

際際滷Share a Scribd company logo
11
Matlab Programming Tips
Part 1 Scripts and Functions
by
Shameer Koya
Introduction
 You can perform operations in MATLAB in two
ways:
1. In the interactive mode, in which all commands are
entered directly in the Command window
2. By running a MATLAB program stored in script file. This
type of file contains MATLAB commands, so running it is
equivalent to typing all the commandsone at a timeat
the Command window prompt. You can run the file by
typing its name at the Command window prompt.
The MATLAB Editor/Debugger Window
Programming in MATLAB
 There are two types of MATLAB programs
Al-Amer 2006
4
% script file
P=[1 3 2]
roots(P)
function [y]=fun(x)
y=x^2+3*x^2+2
script files function files
Script files
 List of MATLAB statements
 Variables are global
 Run it by typing the file
name
Function files
 Starts with function
 List of MATLAB statements
 Variables are local
Script
 .m files
 Both functions and scripts are stored in .m files
 Type up a bunch of commands and save as filename.m
 Type filename in command window to run
 Example: first_program.m
 The name of a script file must begin with a letter, and may include digits
and the underscore character, up to 63 characters.
 Do not give a script file the same name as a variable.
 Do not give a script file the same name as a MATLAB command or
function.
5
Function
 Functions are more complex than scripts
 Functions have their own local variables
 Functions return output as specified, and can accept
input as specified
6
function [FC]=factorial(N)
FC=1;
for i=1:N
FC=FC*i;
end
6
output Function name
input
First statement
must start with
function
Programming Style
 Comments section
a. The name of the program and any key words in the first line.
b. The date created, and the creators' names in the second line.
c. The definitions of the variable names for every input and output
variable.
d. The name of every user-defined function called by the program.
 Input section
Include input data and/or the input functions and comments for
documentation.
 Calculation section
 Output section
This section might contain functions for displaying the output on
the screen.
Example of a Script File
 Problem: Plot a curve of Ohms law verification for a Resistor.
 % Program ohms_law.m Plots curve of current versus
voltage.
 % Created on March 1, 2009 by W. Palm
 % i is current(in ampere)
 % v is voltage (in volt)
 % Input section:
 i = input(Enter the current value:);
 v = input(Enter the voltage value:);
 % Calculation section:
 R = v/i; % Compute the resistance.
 % Output section:
 fprintf('The resistance is %d',R)
Commenting
 Comment your code!
 Any line starting with % is a comment
 Comments can be added to the end of existing lines
by adding a %
 Note that anything after % will be ignored
 >>% This is a comment.
 >>x = 2+3 % So is this.
 In editor screen comments are green
 Any comments written at the beginning of an m-file
will be displayed by the command help filename
9
Commenting - An Example
function [FC]=factorial(N)
% [FC]=factorial(N)
% program to calculate the factorial of a number
% input N : an integer
% if N is not an integer the program obtains the
% factorial of the integer part of N
% output FC : the factorial of N
%
FC=1; % initial value of FC
for i=1:N
FC=FC*i; % n! =(n-1)!*n
end
10
These
comments
will be
displayed
when
help factorial
is typed
Comments are used to explain
MATLAB statements

More Related Content

What's hot (20)

User defined Functions in MATLAB Part 1
User defined Functions in MATLAB Part 1User defined Functions in MATLAB Part 1
User defined Functions in MATLAB Part 1
Shameer Ahmed Koya
Basics of programming in matlab
Basics of programming in matlabBasics of programming in matlab
Basics of programming in matlab
AKANKSHA GUPTA
Matlab programming project
Matlab programming projectMatlab programming project
Matlab programming project
Assignmentpedia
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
Ameen San
Matlab 1(operations on_matrix)
Matlab 1(operations on_matrix)Matlab 1(operations on_matrix)
Matlab 1(operations on_matrix)
harman kaur
User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4
Shameer Ahmed Koya
Working with functions in matlab
Working with functions in matlabWorking with functions in matlab
Working with functions in matlab
harman kaur
Anonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLABAnonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLAB
Shameer Ahmed Koya
Brief Introduction to Matlab
Brief  Introduction to MatlabBrief  Introduction to Matlab
Brief Introduction to Matlab
Tariq kanher
MATLAB BASICS
MATLAB BASICSMATLAB BASICS
MATLAB BASICS
butest
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulinkMATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
reddyprasad reddyvari
C standard library functions
C standard library functionsC standard library functions
C standard library functions
Vaishnavee Sharma
Java 8
Java 8Java 8
Java 8
Sheeban Singaram
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
Ravikiran A
Matlab commands
Matlab commandsMatlab commands
Matlab commands
Tarun Gehlot
C programming session5
C programming  session5C programming  session5
C programming session5
Keroles karam khalil
Matlab practical and lab session
Matlab practical and lab sessionMatlab practical and lab session
Matlab practical and lab session
Dr. Krishna Mohbey
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
amanabr
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
Vikram Nandini
Matlab Introduction
Matlab IntroductionMatlab Introduction
Matlab Introduction
Daniel Moore
User defined Functions in MATLAB Part 1
User defined Functions in MATLAB Part 1User defined Functions in MATLAB Part 1
User defined Functions in MATLAB Part 1
Shameer Ahmed Koya
Basics of programming in matlab
Basics of programming in matlabBasics of programming in matlab
Basics of programming in matlab
AKANKSHA GUPTA
Matlab programming project
Matlab programming projectMatlab programming project
Matlab programming project
Assignmentpedia
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
Ameen San
Matlab 1(operations on_matrix)
Matlab 1(operations on_matrix)Matlab 1(operations on_matrix)
Matlab 1(operations on_matrix)
harman kaur
User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4
Shameer Ahmed Koya
Working with functions in matlab
Working with functions in matlabWorking with functions in matlab
Working with functions in matlab
harman kaur
Anonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLABAnonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLAB
Shameer Ahmed Koya
Brief Introduction to Matlab
Brief  Introduction to MatlabBrief  Introduction to Matlab
Brief Introduction to Matlab
Tariq kanher
MATLAB BASICS
MATLAB BASICSMATLAB BASICS
MATLAB BASICS
butest
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulinkMATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
reddyprasad reddyvari
C standard library functions
C standard library functionsC standard library functions
C standard library functions
Vaishnavee Sharma
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
Ravikiran A
Matlab commands
Matlab commandsMatlab commands
Matlab commands
Tarun Gehlot
Matlab practical and lab session
Matlab practical and lab sessionMatlab practical and lab session
Matlab practical and lab session
Dr. Krishna Mohbey
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
amanabr
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
Vikram Nandini
Matlab Introduction
Matlab IntroductionMatlab Introduction
Matlab Introduction
Daniel Moore

Similar to Matlab Programming Tips Part 1 (20)

User defined functions in matlab
User defined functions in  matlabUser defined functions in  matlab
User defined functions in matlab
Infinity Tech Solutions
Chap2programing.pptxdxnDSnfkezjnfqjdsckjqds
Chap2programing.pptxdxnDSnfkezjnfqjdsckjqdsChap2programing.pptxdxnDSnfkezjnfqjdsckjqds
Chap2programing.pptxdxnDSnfkezjnfqjdsckjqds
yassinebrh31
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptxIIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
rajkumar490591
Palm m3 chapter1b
Palm m3 chapter1bPalm m3 chapter1b
Palm m3 chapter1b
Juan Pablo Fuentes Encinas
Chapter 1.ppt
Chapter 1.pptChapter 1.ppt
Chapter 1.ppt
Indra Hermawan
1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx
MOHAMMAD SAYDUL ALAM
Vb (1)
Vb (1)Vb (1)
Vb (1)
Rajeev Sharan
Algorithm and c language
Algorithm and c languageAlgorithm and c language
Algorithm and c language
kamalbeydoun
Chapter3
Chapter3Chapter3
Chapter3
Kamran
matlabchapter1.ppt
matlabchapter1.pptmatlabchapter1.ppt
matlabchapter1.ppt
PariaMotahari1
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
Durga Padma
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
Vivek Singh Chandel
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
TejaswiB4
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
TejaswiB4
Book management system
Book management systemBook management system
Book management system
SHARDA SHARAN
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
Sivakumar R D .
C programming course material
C programming course materialC programming course material
C programming course material
Ranjitha Murthy
Matlab-3.pptx
Matlab-3.pptxMatlab-3.pptx
Matlab-3.pptx
aboma2hawi
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
indrasir
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
Tarun Gehlot
Chap2programing.pptxdxnDSnfkezjnfqjdsckjqds
Chap2programing.pptxdxnDSnfkezjnfqjdsckjqdsChap2programing.pptxdxnDSnfkezjnfqjdsckjqds
Chap2programing.pptxdxnDSnfkezjnfqjdsckjqds
yassinebrh31
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptxIIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
rajkumar490591
1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx
MOHAMMAD SAYDUL ALAM
Algorithm and c language
Algorithm and c languageAlgorithm and c language
Algorithm and c language
kamalbeydoun
Chapter3
Chapter3Chapter3
Chapter3
Kamran
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
Vivek Singh Chandel
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
TejaswiB4
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
TejaswiB4
Book management system
Book management systemBook management system
Book management system
SHARDA SHARAN
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
Sivakumar R D .
C programming course material
C programming course materialC programming course material
C programming course material
Ranjitha Murthy
Matlab-3.pptx
Matlab-3.pptxMatlab-3.pptx
Matlab-3.pptx
aboma2hawi
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
indrasir
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
Tarun Gehlot

More from Shameer Ahmed Koya (6)

MATLAB Programming - Loop Control Part 2
MATLAB Programming - Loop Control Part 2MATLAB Programming - Loop Control Part 2
MATLAB Programming - Loop Control Part 2
Shameer Ahmed Koya
Matlab Script - Loop Control
Matlab Script - Loop ControlMatlab Script - Loop Control
Matlab Script - Loop Control
Shameer Ahmed Koya
Conditional Control in MATLAB Scripts
Conditional Control in MATLAB ScriptsConditional Control in MATLAB Scripts
Conditional Control in MATLAB Scripts
Shameer Ahmed Koya
Polynomials and Curve Fitting in MATLAB
Polynomials and Curve Fitting in MATLABPolynomials and Curve Fitting in MATLAB
Polynomials and Curve Fitting in MATLAB
Shameer Ahmed Koya
MATLAB - Aplication of Arrays and Matrices in Electrical Systems
MATLAB - Aplication of Arrays and Matrices in Electrical SystemsMATLAB - Aplication of Arrays and Matrices in Electrical Systems
MATLAB - Aplication of Arrays and Matrices in Electrical Systems
Shameer Ahmed Koya
MATLAB - Arrays and Matrices
MATLAB - Arrays and MatricesMATLAB - Arrays and Matrices
MATLAB - Arrays and Matrices
Shameer Ahmed Koya
MATLAB Programming - Loop Control Part 2
MATLAB Programming - Loop Control Part 2MATLAB Programming - Loop Control Part 2
MATLAB Programming - Loop Control Part 2
Shameer Ahmed Koya
Matlab Script - Loop Control
Matlab Script - Loop ControlMatlab Script - Loop Control
Matlab Script - Loop Control
Shameer Ahmed Koya
Conditional Control in MATLAB Scripts
Conditional Control in MATLAB ScriptsConditional Control in MATLAB Scripts
Conditional Control in MATLAB Scripts
Shameer Ahmed Koya
Polynomials and Curve Fitting in MATLAB
Polynomials and Curve Fitting in MATLABPolynomials and Curve Fitting in MATLAB
Polynomials and Curve Fitting in MATLAB
Shameer Ahmed Koya
MATLAB - Aplication of Arrays and Matrices in Electrical Systems
MATLAB - Aplication of Arrays and Matrices in Electrical SystemsMATLAB - Aplication of Arrays and Matrices in Electrical Systems
MATLAB - Aplication of Arrays and Matrices in Electrical Systems
Shameer Ahmed Koya
MATLAB - Arrays and Matrices
MATLAB - Arrays and MatricesMATLAB - Arrays and Matrices
MATLAB - Arrays and Matrices
Shameer Ahmed Koya

Recently uploaded (20)

Chapter 3. Social Responsibility and Ethics in Strategic Management.pptx
Chapter 3. Social Responsibility and Ethics in Strategic Management.pptxChapter 3. Social Responsibility and Ethics in Strategic Management.pptx
Chapter 3. Social Responsibility and Ethics in Strategic Management.pptx
Rommel Regala
How to Configure Restaurants in Odoo 17 Point of Sale
How to Configure Restaurants in Odoo 17 Point of SaleHow to Configure Restaurants in Odoo 17 Point of Sale
How to Configure Restaurants in Odoo 17 Point of Sale
Celine George
FESTIVAL: SINULOG & THINGYAN-LESSON 4.pptx
FESTIVAL: SINULOG & THINGYAN-LESSON 4.pptxFESTIVAL: SINULOG & THINGYAN-LESSON 4.pptx
FESTIVAL: SINULOG & THINGYAN-LESSON 4.pptx
DanmarieMuli1
Fuel part 1.pptx........................
Fuel part 1.pptx........................Fuel part 1.pptx........................
Fuel part 1.pptx........................
ksbhattadcm
TRANSFER OF PATIENTS IN HOSPITAL SETTING.pptx
TRANSFER OF PATIENTS IN HOSPITAL SETTING.pptxTRANSFER OF PATIENTS IN HOSPITAL SETTING.pptx
TRANSFER OF PATIENTS IN HOSPITAL SETTING.pptx
PoojaSen20
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
Digital Tools with AI for e-Content Development.pptx
Digital Tools with AI for e-Content Development.pptxDigital Tools with AI for e-Content Development.pptx
Digital Tools with AI for e-Content Development.pptx
Dr. Sarita Anand
Modeling-Simple-Equation-Using-Bar-Models.pptx
Modeling-Simple-Equation-Using-Bar-Models.pptxModeling-Simple-Equation-Using-Bar-Models.pptx
Modeling-Simple-Equation-Using-Bar-Models.pptx
maribethlacno2
SOCIAL CHANGE(a change in the institutional and normative structure of societ...
SOCIAL CHANGE(a change in the institutional and normative structure of societ...SOCIAL CHANGE(a change in the institutional and normative structure of societ...
SOCIAL CHANGE(a change in the institutional and normative structure of societ...
DrNidhiAgarwal
Rass MELAI : an Internet MELA Quiz Prelims - El Dorado 2025
Rass MELAI : an Internet MELA Quiz Prelims - El Dorado 2025Rass MELAI : an Internet MELA Quiz Prelims - El Dorado 2025
Rass MELAI : an Internet MELA Quiz Prelims - El Dorado 2025
Conquiztadors- the Quiz Society of Sri Venkateswara College
Research & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptxResearch & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptx
Dr. Sarita Anand
Blind spots in AI and Formulation Science, IFPAC 2025.pdf
Blind spots in AI and Formulation Science, IFPAC 2025.pdfBlind spots in AI and Formulation Science, IFPAC 2025.pdf
Blind spots in AI and Formulation Science, IFPAC 2025.pdf
Ajaz Hussain
Information Technology for class X CBSE skill Subject
Information Technology for class X CBSE skill SubjectInformation Technology for class X CBSE skill Subject
Information Technology for class X CBSE skill Subject
VEENAKSHI PATHAK
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
Association for Project Management
Adventure Activities Final By H R Gohil Sir
Adventure Activities Final By H R Gohil SirAdventure Activities Final By H R Gohil Sir
Adventure Activities Final By H R Gohil Sir
GUJARATCOMMERCECOLLE
Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1...
Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1...Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1...
Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1...
pinkdvil200
The Broccoli Dog's inner voice (look A)
The Broccoli Dog's inner voice  (look A)The Broccoli Dog's inner voice  (look A)
The Broccoli Dog's inner voice (look A)
merasan
Principle and Practices of Animal Breeding || Boby Basnet
Principle and Practices of Animal Breeding || Boby BasnetPrinciple and Practices of Animal Breeding || Boby Basnet
Principle and Practices of Animal Breeding || Boby Basnet
Boby Basnet
Computer Application in Business (commerce)
Computer Application in Business (commerce)Computer Application in Business (commerce)
Computer Application in Business (commerce)
Sudar Sudar
English 4 Quarter 4 Week 4 Classroom Obs
English 4 Quarter 4 Week 4 Classroom ObsEnglish 4 Quarter 4 Week 4 Classroom Obs
English 4 Quarter 4 Week 4 Classroom Obs
NerissaMendez1
Chapter 3. Social Responsibility and Ethics in Strategic Management.pptx
Chapter 3. Social Responsibility and Ethics in Strategic Management.pptxChapter 3. Social Responsibility and Ethics in Strategic Management.pptx
Chapter 3. Social Responsibility and Ethics in Strategic Management.pptx
Rommel Regala
How to Configure Restaurants in Odoo 17 Point of Sale
How to Configure Restaurants in Odoo 17 Point of SaleHow to Configure Restaurants in Odoo 17 Point of Sale
How to Configure Restaurants in Odoo 17 Point of Sale
Celine George
FESTIVAL: SINULOG & THINGYAN-LESSON 4.pptx
FESTIVAL: SINULOG & THINGYAN-LESSON 4.pptxFESTIVAL: SINULOG & THINGYAN-LESSON 4.pptx
FESTIVAL: SINULOG & THINGYAN-LESSON 4.pptx
DanmarieMuli1
Fuel part 1.pptx........................
Fuel part 1.pptx........................Fuel part 1.pptx........................
Fuel part 1.pptx........................
ksbhattadcm
TRANSFER OF PATIENTS IN HOSPITAL SETTING.pptx
TRANSFER OF PATIENTS IN HOSPITAL SETTING.pptxTRANSFER OF PATIENTS IN HOSPITAL SETTING.pptx
TRANSFER OF PATIENTS IN HOSPITAL SETTING.pptx
PoojaSen20
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
Digital Tools with AI for e-Content Development.pptx
Digital Tools with AI for e-Content Development.pptxDigital Tools with AI for e-Content Development.pptx
Digital Tools with AI for e-Content Development.pptx
Dr. Sarita Anand
Modeling-Simple-Equation-Using-Bar-Models.pptx
Modeling-Simple-Equation-Using-Bar-Models.pptxModeling-Simple-Equation-Using-Bar-Models.pptx
Modeling-Simple-Equation-Using-Bar-Models.pptx
maribethlacno2
SOCIAL CHANGE(a change in the institutional and normative structure of societ...
SOCIAL CHANGE(a change in the institutional and normative structure of societ...SOCIAL CHANGE(a change in the institutional and normative structure of societ...
SOCIAL CHANGE(a change in the institutional and normative structure of societ...
DrNidhiAgarwal
Research & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptxResearch & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptx
Dr. Sarita Anand
Blind spots in AI and Formulation Science, IFPAC 2025.pdf
Blind spots in AI and Formulation Science, IFPAC 2025.pdfBlind spots in AI and Formulation Science, IFPAC 2025.pdf
Blind spots in AI and Formulation Science, IFPAC 2025.pdf
Ajaz Hussain
Information Technology for class X CBSE skill Subject
Information Technology for class X CBSE skill SubjectInformation Technology for class X CBSE skill Subject
Information Technology for class X CBSE skill Subject
VEENAKSHI PATHAK
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
Association for Project Management
Adventure Activities Final By H R Gohil Sir
Adventure Activities Final By H R Gohil SirAdventure Activities Final By H R Gohil Sir
Adventure Activities Final By H R Gohil Sir
GUJARATCOMMERCECOLLE
Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1...
Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1...Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1...
Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1...
pinkdvil200
The Broccoli Dog's inner voice (look A)
The Broccoli Dog's inner voice  (look A)The Broccoli Dog's inner voice  (look A)
The Broccoli Dog's inner voice (look A)
merasan
Principle and Practices of Animal Breeding || Boby Basnet
Principle and Practices of Animal Breeding || Boby BasnetPrinciple and Practices of Animal Breeding || Boby Basnet
Principle and Practices of Animal Breeding || Boby Basnet
Boby Basnet
Computer Application in Business (commerce)
Computer Application in Business (commerce)Computer Application in Business (commerce)
Computer Application in Business (commerce)
Sudar Sudar
English 4 Quarter 4 Week 4 Classroom Obs
English 4 Quarter 4 Week 4 Classroom ObsEnglish 4 Quarter 4 Week 4 Classroom Obs
English 4 Quarter 4 Week 4 Classroom Obs
NerissaMendez1

Matlab Programming Tips Part 1

  • 1. 11 Matlab Programming Tips Part 1 Scripts and Functions by Shameer Koya
  • 2. Introduction You can perform operations in MATLAB in two ways: 1. In the interactive mode, in which all commands are entered directly in the Command window 2. By running a MATLAB program stored in script file. This type of file contains MATLAB commands, so running it is equivalent to typing all the commandsone at a timeat the Command window prompt. You can run the file by typing its name at the Command window prompt.
  • 4. Programming in MATLAB There are two types of MATLAB programs Al-Amer 2006 4 % script file P=[1 3 2] roots(P) function [y]=fun(x) y=x^2+3*x^2+2 script files function files Script files List of MATLAB statements Variables are global Run it by typing the file name Function files Starts with function List of MATLAB statements Variables are local
  • 5. Script .m files Both functions and scripts are stored in .m files Type up a bunch of commands and save as filename.m Type filename in command window to run Example: first_program.m The name of a script file must begin with a letter, and may include digits and the underscore character, up to 63 characters. Do not give a script file the same name as a variable. Do not give a script file the same name as a MATLAB command or function. 5
  • 6. Function Functions are more complex than scripts Functions have their own local variables Functions return output as specified, and can accept input as specified 6 function [FC]=factorial(N) FC=1; for i=1:N FC=FC*i; end 6 output Function name input First statement must start with function
  • 7. Programming Style Comments section a. The name of the program and any key words in the first line. b. The date created, and the creators' names in the second line. c. The definitions of the variable names for every input and output variable. d. The name of every user-defined function called by the program. Input section Include input data and/or the input functions and comments for documentation. Calculation section Output section This section might contain functions for displaying the output on the screen.
  • 8. Example of a Script File Problem: Plot a curve of Ohms law verification for a Resistor. % Program ohms_law.m Plots curve of current versus voltage. % Created on March 1, 2009 by W. Palm % i is current(in ampere) % v is voltage (in volt) % Input section: i = input(Enter the current value:); v = input(Enter the voltage value:); % Calculation section: R = v/i; % Compute the resistance. % Output section: fprintf('The resistance is %d',R)
  • 9. Commenting Comment your code! Any line starting with % is a comment Comments can be added to the end of existing lines by adding a % Note that anything after % will be ignored >>% This is a comment. >>x = 2+3 % So is this. In editor screen comments are green Any comments written at the beginning of an m-file will be displayed by the command help filename 9
  • 10. Commenting - An Example function [FC]=factorial(N) % [FC]=factorial(N) % program to calculate the factorial of a number % input N : an integer % if N is not an integer the program obtains the % factorial of the integer part of N % output FC : the factorial of N % FC=1; % initial value of FC for i=1:N FC=FC*i; % n! =(n-1)!*n end 10 These comments will be displayed when help factorial is typed Comments are used to explain MATLAB statements