Introduction To MATLAB: Kathmandu University
Introduction To MATLAB: Kathmandu University
Introduction To MATLAB: Kathmandu University
Kathmandu University
Mathematics Group
1 M-files
MATLAB has two M-files. They are script files and function files.
Both are called M-files, because they have a file extension of .m.
Where to type? Select new M-file from the file menu. A new edit
window will appear. Type M-files in this window.
Scripts files:
A script file is a user-created file with a sequence of MATLAB command
in it. It is equivalent to typing all the commands stored in the script
file, one by one, at the MATLAB prompt in the command window.
The file must be saved with a .m extension to its name, thereby,
making it an M-file. The file name must begin with a letter and the
rest of the character may include digits and underscores but not period
other than the last one .m.
The script file can execute by typing its name (without the .m ex-
tension)in the command window.
Script files work on global variables, that is, variables currently
present in the workspace. Results obtained from executing script files
are left in the workspace.
Script files are useful when we have to repeat a set of commands several
times.
Script files may be used to enter a data into huge matrices, since entry
error can be easily corrected.
1
my_val = input(Enter the input value: );
If the input function includes the character s as a second argument, then the
input data is returned to the user as a character string. Thus, the statement
disp function:
The disp function displays the data. The disp function accepts an array
argument, and displays the value of the array in the command window. If the
array is of type char, then character string contained in the array is printed
out.
This function is often combined with the functions num2str (converts
a number to string) and int2str (converts an integer to string) to create
message to be displayed in the command window. For example, the following
MATLAB statements
will display The value of pi = 3.1416 in the command window. The first
statement creates a string array containing the message, and the second
statement displays the message.
fprintf function:
An even more flexible way to display data is with the fprintf function.
The fprintf function displays one or more values together with related text
and lets the programmer control the way the displayed values appear. The
general form of this function when it is used to print to the command window
is
fprintf(format, data)
where format is a string describing the way the data is to be printed, and
data is one or more scalars or arrays to be printed. The format is a character
string containing text to be printed plus special characters describing the
format of the data. For example, the function
2
will print out The value of pi is 3.141593 followed by a line feed. The
character %f is a conversion character. It indicates that the value in the data
list should be printed out in floating point format at that location in the
format string. The character \ n is an escape character. It indicates that
a line feed should be issued so that the following text starts on a new line.
Other characters are as shown in table below.
will print out The value of pi is 3.14 followed by a line feed. The
conversion %8.2f indicates that the first data item in the function should be
printed out in floating point format in a field eight characters wide, including
two digits after the decimal point.
>> x = 2 + 3*i;
>> str = [disp: x = num2str(x)];
>> disp(str);
>> fprintf(fprintf: x = %8.4f \n , x);
Problem 1 Write a script file to draw a unit circle whose parametric equa-
tion is given by
x = cos(), y = sin (), 0 2
Solution steps:
Select New M-file from the file menu. A new edit window will appear.
Type the following lines into this edit window. Lines starting with %
sign are interpreted as comment line by MATLAB and are ignored.
3
%
% File written to give idea to students how to creat a script file.
% Dated on Feb 4, 2008.
%
% Define Variables:
% theta --- angle in radian.
% x --- x = cos(theta).
% y --- y = sin(theta).
%--------------------------------------------------------------------------
% Create array for input variables
theta = linspace(0, 2*pi, 100); % creat vector data
x = cos(theta); % generates x - coordinates
y = sin(theta); % generates y - coordinates
% Creates plot
plot(x,y); % plot the unit circle
axis(equal); % set equal scale on axes
title(Circle of unit radius); % put a title
text(0.5,0.5,note this); % put text at specified point
Select save as from file menu. A diaglog box will appear. Type the name
of the document as circle.m in the folder you want to be. Then click
yes to save the file.
Now to get back to MATLAB type the following in the command win-
dow.
4
The function file must be saved in your required folder with the file
name fname.m.
The function file can execute by giving the values of the input argu-
ments by typing the format of function file in the command window.
Select New M-file from the file menu. A new edit window will appear.
Type the following lines into this edit window. Lines starting with %
sign are interpreted as comment line by MATLAB and are ignored.
Select save as from file menu. A diaglog box will appear. Type the name
of the document as circlefn.m in the folder you want to be. Then click
yes to save the file.
Now to get back to MATLAB type the following in the command win-
dow.