Sie sind auf Seite 1von 12

Suman Senden M-Files in MATLAB

Suman Senden
rd
M.Sc. Engineering Geology, 3 Semester
Roll Number:08/2075

4Th Sep., 2020


Suman Senden M-Files in MATLAB

Introduction to MATLAB M-Files


An m-file is a simple text file where we can
place MATLAB commands. When the file is
run, MATLAB reads the commands and executes them
exactly as it would if we had typed each command
sequentially at the MATLAB prompt. All m-file names
must end with the extension '. m' (e.g. test. m).

4Th Sep., 2020


Suman Senden M-Files in MATLAB

Scripts − script files are program Functions − functions


files with .m extension. In these files are also program
files, we write series of files with .m
commands, which we want to extension. Functions
execute together. Scripts do not can accept inputs and
accept inputs and do not return return outputs. Internal
any outputs. They operate on variables are local to
data in the workspace. the function.

4Th Sep., 2020


Suman Senden M-Files in MATLAB

Scripts versus Functions


 The biggest difference between scripts and functions is that
functions have input and output parameters whereas Scripts
do not accept inputs and do not return any outputs.
 Script files can only operate on the variables that are hard-coded
into their m-file.
 They are therefore more suitable for general purpose tasks that
will be applied to different data.
 Scripts are useful for tasks that don't change.
 They are also a way to document a specific sequence of actions,
say a function call with special parameter values, that may be
hard to remember

4Th Sep., 2020


Suman Senden M-Files in MATLAB

A script file contains multiple sequential lines of MATLAB commands and function calls. You can run a script by typing
its name at the command line.

To create scripts files, we need to use a text editor. We can open the MATLAB editor in two ways −

If we are using the command prompt, type edit in the command prompt. This will open the editor. You
can directly type edit and then the filename (with .m extension)

4Th Sep., 2020


Suman Senden M-Files in MATLAB

•Matlab function files are very similar to Matlab script files, with a few important
differences.
•Both are plain ASCII( American Standard Code for Information Interchange) files
containing Matlab commands, with ".m" file extensions.
•Ordinary script files have the problem that they run in the scope of the command
window. Any variables created get added to the workspace, and may replace other
variables that were already defined there with the same names.
•Variables defined and used in a function do not affect the regular Matlab workspace.
•As a consequence, functions have mechanisms for passing arguments into the
functions, and for returning the results.

4Th Sep., 2020


Suman Senden M-Files in MATLAB

Syntax
The general syntax of a Matlab function is as follows:
function [ outarg1, outarg2, ..., outargN ] = functionName( inarg1, inarg2, ..., inargM )

Matlab commands : Argument


outarg1 = . . . . .,
outargN = . . . . . .
.
.
.

4Th Sep., 2020


Suman Senden M-Files in MATLAB

When to use Scripts Vs. Functions


We use scripts when we want to…
Make small calculations(e.g. factorial, plotting, basic computing etc.

We use functions when we want to…


Calculate values (r) as a function of variables (T,Y,…):=f(t,y,…)
Pass on the function values to MATLAB function for solving something;
Calculate properties as a function of temperature, concerntration, current,
etc.

4Th Sep., 2020


Suman Senden M-Files in MATLAB

Recap
Script M-files =Text file containing MATLAB commends
Can save, share, edit M-files.
The name of the function file must be the name of the
function i.e. function myfile_name

4Th Sep., 2020


Suman Senden M-Files in MATLAB

Thank you for your patience!



I appreciate for your questions!!

4Th Sep., 2020

Das könnte Ihnen auch gefallen