CL2014
MATLAB Programming
Lecture: 02
Week: 02
Course Instructor
Engr Muhammad Umar Khan Niazi
Office: C1-21, Ext.: 218, Email: [email protected]
1
2 Course Objectives
Working in command window Runtime results
Working in command window is useful for simple problems
However,
Work is not saved for later use.
Not easy to make changes.
3 m-files in MATLAB
For Programing in MATLAB ‘Editor window’ is used to write and save the programs.
File is save as filename.m therefore called as m-files
File names must start with a letter, and can contain letters, digits, or underscores.
Avoid using accent characters such as umlauts or circumflexes in path names. MATLAB might not
recognize the path. In addition, attempts to save a file to such a path might result in unpredictable
behavior.
If a path or file name contains spaces, enclose the input in single quotes. For example:
load 'filename with space.mat'
or
Load ('filename with space.mat')
T1_22L7123
4 m-files in MATLAB
The extension of MATLAB files is .m therefore called as m-files
‘Editor Window’ is use to create the m-files.
By default m-files are saved in current working directory.
Like any other computer files, m files can be open/edit/save at
anytime.
Practical demonstration
Create m-file save m-file Edit m-file.
5 Commands for Managing Work sessions
clc Clear the command window
clear Clear all variables
clear var1, var2 Clear selective variables
format Set the format to display the significant digits (in
results)
% Comment
exist(‘var1’) To check either a variable with name var1 exist or
not.
; Suppresses screen printing
dir List the files in current directory
6 ‘Disp’ Command
In MATLAB, disp command is use to display in command
Window
disp(‘string’) is to display the strings
disp(var1) is to display the value of var1
7 Num2Str
To display the String and variable together is same line.
First, need to make a string by using num2str(var1)
8 Input Command in MATLAB
There are multiple ways to get the input in MATLAB
The simplest way to get the input form the user during run time) is by
using INPUT command
9 Input Command in MATLAB
In general ‘Input’ is use to get value of a variable during Runtime
Var1 = input(‘Message String’)
Once the control will reach to this line, the message string will be
displayed for the information of user
The value will given by the user and followed by the enter key.
The value will be stored in Var1
10 Input Command in MATLAB
N O TE: Message will
appear in
command Window
11 Example of mfile