You are on page 1of 19

MONASH

ENGINEERING
ENG1060

INPUT AND OUTPUT


Presented by Tony Vo
Slides by Tony Vo
ENG1060

THE ROAD THUS FAR…

1. Introduction to ENG1060
2. MATLAB basics
3. Matrices
4. Plotting
5. Good programming practices
6. Functions
7. Input and output
8. IF statements
9. Loops, loops, loops…
10. Debugging MATLAB programs
11. Advanced functions
12. Data types and MATLAB limitations
2
ENG1060

LABS

 Labs become progressively harder


– Preparation is required
– Start the next lab if you've finished early

 Ensure you come to the lab on time


– Do not come to the lab in the final hour thinking you can just get marked off

 First 3 labs marks should be uploaded by the end of this week


– Marks uploaded weekly thereafter

3
ENG1060

WHAT THE DEMONSTRATORS ARE SAYING

 Students
– Are not preparing for the labs
– Hard coding and not creating variables
– Still don't know how to use the min/max functions
(Preaching to the wrong audience?)
– Are overall better than previous semesters

 Please bear in mind (hello Lisa )


– Part A is still in development (videos, workshops, labs, supplementary questions)

4
ENG1060

RECAP: DATA TYPE: STRINGS

 MATLAB can provide many data types


– So far, we have mainly used doubles

 A string is another data type


– Strings are used to store characters
– Also called ''character strings'' or ''char array''

 Examples:
A = 'abcdef'
B = '1a2b3c'
C = '%^&*#^&%$'
5
ENG1060

RECAP: DATA TYPE: STRINGS

 You would have used strings to label your plots


– xlabel('string')
– ylabel('string')
– title('string')
– legend('string')

 Or even in function arguments


– input('string')
– disp('string')
– plot(x,y,'string')

6
ENG1060

RECAP: STRINGS VS. NUMBERS

 Computers store characters using numbers related to ASCII


– ASCII stands for American Standard Code for Information Interchange
– Computers only understand numbers
– Numerical representation of a character such as 'a' or '@'

 If unsure, check the data type


– Use the whos command or look through the workspace window
– Char = character string
– Double = numerical value

7
ENG1060

RECAP: ASCII TABLE

8
ENG1060

RECAP: FPRINTF & SPRINTF

 Syntax: fprintf('format', var1, var2, var3, …)


sprint('format', var1, var2, var3, …)

 First argument: is a string containing placeholders for your variables


– Placeholders are denoted with a percentage symbol (%)
– Placeholders are coupled with specifiers

 Subsequent arguments: are the variables for your placeholders


– The list of variables appear in the order written

9
ENG1060

RECAP: FPRINTF/SPRINTF: CONVERSION SPECIFIERS

 Conversion specifiers tell fprintf where and how variable values should be
printed
%printing information
fprintf('The length of the hypotenuse is given by %f', hyp_length)

10
ENG1060

EXAMPLE 1

Plot the given time and speed vectors. Determine the maximum point and mark
it on the same plot.
t = 70:10:150;
s = -(t-100)2 + 2500;

Use fprintf to output the maximum speed and


corresponding time

11
ENG1060

EXAMPLE 2

A torus is a shaped like a doughnut. If its inner radius is ‘a’ and its outer radius is
‘b’, its volume and surface area are given by:
1
𝑉 = 𝜋 2 𝑎 + 𝑏 𝑏 − 𝑎 2; A = 𝜋 2 𝑏 2 − 𝑎2 ;
4

Create a function that computes V and A

Suppose the outer radius is to be 2 inch greater than the inner


radius. Calculate V and A for 0.25 ≤ a ≤ 4 with increments
of 0.05 inches.

Plot the results with the grid turned on.


12
ENG1060

EXAMPLE 2

A torus is a shaped like a doughnut. If its inner radius is ‘a’ and its outer radius is
‘b’, its volume and surface area are given by:
1
𝑉 = 𝜋 2 𝑎 + 𝑏 𝑏 − 𝑎 2; A = 𝜋 2 𝑏 2 − 𝑎2 ;
4

Print the following statement:


"The largest radii of a=XX and b=XX has a volume of
V=XX and an area of A=XX"

13
ENG1060

DATA OVERLOAD

 We collect and create a lot of data


– Therefore we need a way to import and export data In MATLAB

 Reigl LMS Z420i laser scanner


– 360 degrees rotation
– 80 degrees tilt
– 10mm or better accuracy
– Range up to 1km
– Each scan returns 16,500,000 data points
– GBs of data per scan

14
ENG1060

RECAP: FOPEN, FCLOSE AND FGETL FUNCTIONS

 fopen: opens a file


– Syntax: file_id = fopen(‘filename’)
– Ensure that the file being opened is in the current directory

 fclose: closes a file


– Syntax: fclose(file_id)

 fgetl: gets line of data from file


– Syntax: data = fgetl(file_id)

15
ENG1060

RECAP: IMPORTDATA FUNCTION

 Use the importdata function to load files containing a lot of data


– The importdata function is different to the import data wizard
Syntax: all_data = importdata('filename')
– No need for fopen and fclose

 Imports the data in as a structure


– Structure is comprised of data, text data and column headers
– Data from structures can be accessed via: content = <matrix>.<content_type>

16
ENG1060

EXAMPLE 3

The "long_jump_records.txt" file contains the top 25 long jump distances


– 1st row: rank #
– 2nd row: distance (m)
– 3rd row: wind speed (m/s)

Import the data and plot distance against


rank. On a new figure plot wind speed
against distance.

17
ENG1060

EXAMPLE 3

Do the same thing but now with the "long_jump_records2.txt" file which has a
different structure using fgetl…

… and importdata

18
ENG1060

PART A: MATLAB PROGRAMMING

1. Introduction to ENG1060
2. MATLAB basics
3. Matrices
4. Plotting
5. Good programming practices
6. Functions
7. Input and output
8. IF statements
9. Loops, loops, loops…
10. Debugging MATLAB programs
11. Advanced functions
12. Data types and MATLAB limitations
19

You might also like