intprog2s

2
Introduction to programming with MATLAB © Bar-Ilan University Computer Center  September 12 1 Session 2 page 1 Introduction to programming with MATLAB Session 2  Loops 2 Loops: Iterative constructs Repe at a group o f commands ma ny times. Purpo se: auto mat ion of proce ssing ma ny sets of data. Only wh en MATL AB cap abil ity of arr ay  processing can not be used on all data a t once. 3 Stopping criterion When a pr e-de fine d set of val ues has be en used. (  for-loop) Acco rding to a log ical c ondi tion (while-loop). continue condition commands T r   u  e F  a l     s  e 4 while loops Synta x: whi l e 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 .  Demo: While loops Question 1 5 for - loops • Syntax: for var = row array commands end Operation:  – 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 Questions 2-3 6 Disk, data and files Data i s stor ed on d isk in file s. A file refers to certain storage space on disk, with the following attributes:  Location on disk.  Name a label used for reference Contents  – the data stored in the file. 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. name contents

Upload: luthfya-umahati

Post on 02-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: intprog2s

8/10/2019 intprog2s

http://slidepdf.com/reader/full/intprog2s 1/2

Introduction to programming with MATLAB © Bar-Ilan University Computer Center  

September 12 1Session 2 page

1

Introduction to programming

with MATLAB

Session 2

 Loops

2

Loops: Iterative constructs

• 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.

3

Stopping criterion• When a pre-defined set of values has been

used. ( for-loop)

• According to a logical condition (while-loop).

continue condition

commands

T r   u e 

F  a l     s  e 

4

while loops• Syntax:

whi l e condition

commandsend

• Operation:

 – If condition is true, commands are executed.

 – Test condition again. If true, commands areexecuted. Repeat until condition is false.

 – If condition is false, continue execution afterend.

 Demo: While loops

Question 1

5

for - loops

• Syntax:fo r var  = row array

commandsend

• Operation:

 – 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

Questions 2-3 6

Disk, data and files

• Data is stored on disk in files.

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

 Location on disk.

 Name – a label used for reference

Contents – the data stored in the file.

• Writing a file: storing data from variables inmemory, to a file on disk.

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

name

contents

Page 2: intprog2s

8/10/2019 intprog2s

http://slidepdf.com/reader/full/intprog2s 2/2

Introduction to programming with MATLAB © Bar-Ilan University Computer Center  

September 12 2Session 2 page

7

File-types• Extension: the last “token” in a file-name.

Example: mydat a.  xls• Text files:

contain only letters (ascii characters).

Are readable by any text editor (e.g. NotePad)

• Binary files: have internal format that can beread only by certain programs.Example: Word documents

• File-types and the programs that can open themare associated with certain extensions.Example: .doc is associated with MS Word.

8

File-types in MATLAB

• 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)

9

Importing data into MATLAB• The load command: l oad f i l ename

• Load binary: filename without extension,assumes . matExample: l oad myvars will look for a filemyvars.mat and will load all variables in it.

• Load text: filename with any extension except. mat will load one matrix.

Example: l oad nonmono. dat will createa variable named nonmono.Functional syntax: a =l oad( ' fname.txt' )

• Excel files: A = xl sr ead( ' file-name' )will read the excel file file-name. 10

Saving data to disk 

• The save command:

save f i l ename vars

• Save variables in binary: assumes . mat .

save f i l ename var1 var2 var3 …

save f i l ename will save all variables.

• Save numerical matrices in readable format:

save name. ext var1 var2 … - asci iwill print var1, var2 etc. into a file, each row

in a new line. Demo: Files

Question 4

11

Leaving a loop• br eak: Leave a loop, and continue

execution after end.

• Commonly used with conditional branching.

fo r var  = row arraycommand1

command2

i f condition

break

end

command3

command4end 12

Skipping current passage

• cont i nue: Skip the rest of commands

until end, and start next iteration.

 Demo: Other forms of branching

fo r var  = row arraycommand1

command2

i f condition

cont i nue

end

command3

command4end