0% found this document useful (0 votes)
9 views8 pages

Lecture 01 - Programming With Matlab

Uploaded by

Yahia Zakaria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

Lecture 01 - Programming With Matlab

Uploaded by

Yahia Zakaria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

M Files

- MATLAB allows you to write series of commands into a


file and execute the file as complete unit, like writing
a function and calling it.

The M Files
- MATLAB allows writing two kinds of program file:
1. Scripts
2. Functions

1
What are scripts?
- Scripts − script files are program files with .m
extension.
- In these files, you write series of commands, which you
want to
execute together.
- Scripts do not accept inputs and do not return any
outputs.
- They operate on data in the workspace.

2
What are
Functions?
- Functions − functionsfiles are also program files
with .m extension.
- Functions can accept inputs and return outputs.
- Internal variables are local to the function.

3
Creating Scripts
- You can use the MATLAB editor or any other text editor
to create your .mfiles.
- A script file contains multiple sequentiallines of
MATLAB
commands and function calls.
- You can run a script by typing its name at the
command line.
- Creating and Running Script File
- To create scripts files, you need to use a text editor.
- if you are using the Matlab IDE, choose NEW -> Script.
- This will open the editor and creates a file named
Untitled. You can name and save the file after typing
the code.

4
Creating Scripts
After creating and saving the file, you can run it in two
ways −
- Clicking the Run button on the editor window or
- Just typing the filename (without extension) in the
command prompt: >> prog1

5
Functions
- A function is a group of statements that together
perform a task.
In MATLAB, functions are defined in separate files.
- The name of the file and of the function should be the
same.
- Functions operate on variables within their own
workspace, which is also called the local workspace,
separate from the workspace you access at the
MATLAB command prompt which is called the base
workspace.

6
Functions
- Functions can accept more than one input arguments
and may return more than one output arguments.
- Syntax of a function statement is −

function [out1,out2, ..., outN] = myfun(in1,in2,in3, ..., inN)

7
Anonymous
Functions
- An anonymous function is like an inline function in
traditional programming languages, defined within
a single MATLAB statement. It consists of a single
MATLAB expression and any number of input and
output arguments.
- Youcan define an anonymous function
right at the MATLAB
command line or within a function or script.
- This way you can create simple functions without
having to create a file for them.
8

You might also like