You are on page 1of 23

Structured Programming and Applications for Building and

Civil Engineers

Lecture 3
by
Dr. Emre Erkmen, P.Eng.

Assistant Professor, Department of Building, Civil and Environmental Engineering,


Concordia University, Montreal

Office: EV 6.209
Phone:514 848 2424
Email: emre.erkmen@Concordia.ca
Lecture hours: Mondays 13:15-16:00
Office hours: Tuesdays 10:00-11:00

1
Structured Programming - Lecture 3

Outline of Today’s Lecture

Introduction to Matlab Programming

- Customized Plots

- Input/Output with Load and save

- User defined functions

- Selection Statements
Structured Programming - Lecture 3

Customized Plots in Matlab

3
Structured Programming - Lecture 3

Input/Output

In many cases, input to a script will come from a data file that has been created by
another source. Also, it is useful to be able to store output in an external file that
can be manipulated and/or printed later.

4
Structured Programming - Lecture 3

Writing data to a file

 Write

 Append

5
Structured Programming - Lecture 3

Read data

6
Structured Programming - Lecture 3

Matlab programs vs. Matlab functions

• Functions can be flexibly called in a code or on the prompt line,


programs normally cannot
• Functions need to have inputs and outputs, programs do not
• A program can include a number of functions, functions can
also include number of other functions but should be linked to
one another through inputs and outputs. Programs can be
absolutely standalone and independent. Functions normally
cannot, at least in the sense that they need inputs.

For simplicity, consider functions as mini-programs

7
Structured Programming - Lecture 3

Example of Built-in functions in Matlab

8
Structured Programming - Lecture 3

User defined functions that return a single value

9
Structured Programming - Lecture 3

Example user-defined functions in Matlab

All functions have their own workspace.


10
Structured Programming - Lecture 3

Calling a function

11
Structured Programming - Lecture 3

Standardization of function introduction

12
Structured Programming - Lecture 3

Functions with multiple arguments

13
Structured Programming - Lecture 3

Functions with local variables


In many cases calculation in a function require extra variables

14
Structured Programming - Lecture 3

Additional notes

15
Structured Programming - Lecture 3

Programming Guidelines

16
Structured Programming - Lecture 3

Selection statements

• In the scripts and functions we’ve seen thus far every statement was executed
in sequence. This is not always desirable.

• We need a mechanism with which we can make choices as to whether


statements are executed or not, and how to choose between or among
statements.

• The statements that accomplish this are called conditional, selection or


branching statements.

17
Structured Programming - Lecture 3

The “IF” statement

The script between keywords “if” and “end”


will be skipped, unless the condition is satisfied

18
Structured Programming - Lecture 3

The “IF-ELSE” statement

19
Structured Programming - Lecture 3

Nested “IF-ELSE” statement

20
Structured Programming - Lecture 3

The “IF-ELSEIF-ELSE” statement

21
Structured Programming - Lecture 3

The “SWITCH-CASE-OTHERWISE” statement

22
Structured Programming - Lecture 3

The “IS” function

23

You might also like