0% found this document useful (0 votes)
35 views

Analysis

The document discusses M-files in MATLAB which are script files that contain MATLAB commands that can be saved and run multiple times. It defines M-files and functions, and describes how to create, save, open and run M-files as well as reasons for using M-files instead of the command window.

Uploaded by

Sangin Zandi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Analysis

The document discusses M-files in MATLAB which are script files that contain MATLAB commands that can be saved and run multiple times. It defines M-files and functions, and describes how to create, save, open and run M-files as well as reasons for using M-files instead of the command window.

Uploaded by

Sangin Zandi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

University Of Garmian

Civil engineering department


Second stage

Report about

(M-file in matlab)

Prepared by :

( Rahand ashraf amir )

Supervised by:
Dr.Rafiq Salih

2022-2023

Page | 1
Table Of contents:

 Introduction
 definition
 M-File functions
 Anatomy of a M-File function:
 Why use m-file ?
 How to create,save or open an m-file?
 How to run m-file
 Script side-effects

Page | 2
 Introduction:

So far in these lab sessions, all the commands were executed in


the Command Window. The problem is that the commands
entered in the Command Window cannot be saved and executed
again for several times. Therefore, a different way of executing
repeatedly commands with MATLAB is: 1. to create a file with a
list of commands, 2. save the file, and 3. run the file. If needed,
corrections or changes can be made to the commands in the file.
The files that are used for this purpose are called script files or
scripts for short. This section covers the following topics:

• M-File Scripts
• M-File Functions

Page | 3
 Definition:

A script file is an external file that contains a sequence of


MATLAB statements. Script files have a filename extension .m
and are often called M-files. M-files can be scripts that simply
execute a series of MATLAB statements, or they can be
functions that can accept arguments and can produce one or
more outputs.

Page | 4
 M-File functions:

As mentioned earlier, functions are programs (or routines)


that accept input arguments and return output arguments.
Each M-file function (or function or M-file for short) has its
own area of workspace, separated from the MATLAB base
workspace.

 Anatomy of a M-File function:

Page | 5
This simple function shows the basic parts of an M-file.
function f = factorial(n) (1)
% FACTORIAL(N) returns the factorial of N. (2)
% Compute a factorial value. (3)
f = prod(1:n); (4)

The first line of a function M-file starts with the keyword function. It
gives the function name and order of arguments. In the case of
function factorial, there are up to one output argument and one
input argument. Table 4.1 summarizes the M-file function. As an
example,
For
n = 5, the result is,
>> f = factorial(5)
f=
120

 Why use m-file ?


Page | 6
For simple problems, entering your requests at the
MATLAB prompt is fast and efficient. However, as the
number of commands increases or trial and error is done by
changing certain variables or values, typing the commands
over and over at the MATLAB prompt becomes tedious. M-
files will be helpful and almost necessary in these cases.

How to create,save or open an m-file?


Page | 7
If you are using PC or Mac:
To create an m-file, choose New from the File menu and select
Script. This procedure brings up a text editor window in which you
can enter MATLAB commands.
To save the m-file, simply go to the File menu and choose Save
(remember to save it with the '.m' extension). To open an existing
m-file, go to the File menu and choose Open.
If you are using Unix:
To create an m-file, use your favorite text editor (pico, nedit, vi,
emacs, etc.) to create a file with .m extension (e.g. filename.m).

 How to run m-file:


Page | 8
After the m-file is saved with the name file name.m in the
current MATLAB folder or directory, you can execute the
commands in the m-file by simply typing filename at the
MATLAB command window prompt.
If you don't want to run the whole m-file, you can just copy
the part of the m-file that you want to run and paste it at
the MATLAB prompt.

Page | 9
 Script side-effects :

All variables created in a script file are added to the workspace.


This may have undesirable effects, because:

• Variables already existing in the workspace may be overwritten.

• The execution of the script can be affected by the state variables in


the workspace.

As a result, because scripts have some undesirable side-effects, it


is better to code any complicated applications using rather function
M-file.

Page | 10

You might also like