Вы находитесь на странице: 1из 2

Introduction to programming with MATLAB

Bar-Ilan University Computer Center

Loops: Iterative constructs


Introduction to programming
with MATLAB

Repeat a group of commands many times.


Purpose: automation of processing many
sets of data.
Only when MATLAB capability of array
processing can not be used on all data at
once.

Session 2
Loops

while loops

Stopping criterion
When a pre-defined set of values has been
used. (for-loop)
According to a logical condition (while-loop).

Syntax:
while condition
commands
end
Operation:
If condition is true, commands are executed.
Test condition again. If true, commands are
executed. Repeat until condition is false.
If condition is false, continue execution after
end.

continue condition

True

False

commands
3

for - loops

Data is stored on disk in files.


A file refers to certain storage space on disk,
with the following attributes:

Operation:

Location on disk.
Name a label used for reference
Contents the data stored in the file.

var is assigned the first value of array.


commands are executed.
var is assigned the next value of array.
commands are executed.
Repeat the same way, with all values of array.
After the last value of array, continue after end.
Demo: For-loops

September 12

Disk, data and files

Syntax:
for var = row array
commands
end

Questions 2-3

Demo: While loops


Question 1

contents

name

Writing a file: storing data from variables in


memory, to a file on disk.
Reading a file: loading data from a file on disk,
to variables in memory.
5

Session 2 page 1

Introduction to programming with MATLAB

Bar-Ilan University Computer Center

File-types in MATLAB

File-types
Extension: the last token in a file-name.
Example: mydata.xls
Text files:
contain only letters (ascii characters).
Are readable by any text editor (e.g. NotePad)

Binary files: have internal format that can be


read only by certain programs.
Example: Word documents
File-types and the programs that can open them
are associated with certain extensions.
Example: .doc is associated with MS Word.

Programs: .m m-files ascii (text)


Variables: .mat MATLAB internal format
Figures: .fig MATLAB internal format
Non-MATLAB formats:
Text (e.g. tables)
Pictures (e.g. .jpg)
Worksheets (e.g. Excel)
Sound (e.g. .wav)

Importing data into MATLAB

Saving data to disk

The load command: load filename


Load binary: filename without extension,
assumes .mat
Example: load myvars will look for a file
myvars.mat and will load all variables in it.
Load text: filename with any extension except
.mat will load one matrix.
Example: load nonmono.dat will create
a variable named nonmono.
Functional syntax: a =load('fname.txt')
Excel files: A = xlsread('file-name')
will read the excel file file-name.
9

Leaving a loop

Question 4

10

continue: Skip the rest of commands


until end, and start next iteration.

Commonly used with conditional branching.

September 12

The save command:


save filename vars
Save variables in binary: assumes .mat.
save filename var1 var2 var3
save filename will save all variables.
Save numerical matrices in readable format:
save name.ext var1 var2 -ascii
will print var1, var2 etc. into a file, each row
in a new line.
Demo: Files

Skipping current passage

break: Leave a loop, and continue


execution after end.
for var = row array
command1
command2
if condition
break
end
command3
command4
end

11

for var = row array


command1
command2
if condition
continue
end
command3
command4
end
Demo: Other forms of branching

12

Session 2 page 2

Вам также может понравиться