Top Banner
Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009
16

Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009.

Dec 26, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009.

Input and Output in MATLAB

Lecture 13: Supporting Material

Dr Kathryn Merrick

Tuesday 14th April, 2009

Page 2: Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009.
Page 3: Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009.

Overview

Gathering user input

Formatting output for display

File input and output

Code clichés for designing a user interface

Page 4: Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009.

An Introductory Problem…

Question:

At 200km/hr, what will stop in a shorter distance, a road car or a F1 race car?

Page 5: Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009.

From Problem to Program…

1. Understand the problem ‘in English’

2. Develop a rough solution on paper

3. Implement the solution in MATLAB

4. Test your solution

Page 6: Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009.

Some Equations for Motion

s is stopping distance u is initial velocity v is final velocity μ is the coefficient of friction

between tyres and road g is gravity t is time a is acceleration g

us

2

2

2

2

1atuts

t

uva

Page 7: Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009.

Demo 1:

Interactive Calculator for Motion in MATLAB

Page 8: Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009.

Gathering User Input

Numbers

time = input('Enter time in seconds: ');

Strings

name = input('Enter your name: ', 's');

Page 9: Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009.

Displaying Simple Output

disp('Error: You must enter a number from 1-3')

disp('Now exiting the program... Goodbye.')

disp(pi)

Page 10: Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009.

Demo 2:

Formatting Output

Page 11: Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009.

Formatting Complex Output

name = 'Kathryn';

office_number = 212;

fprintf('The lecturer''s name is %s.

Her office number is %u.\n',

name,

office_number);

Page 12: Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009.

Useful fprintf Options

%s string

%d decimal (scientific notation)

%f floating point number

%.2f floating point number to 2 decimal places

\n new line

\t tab

Page 13: Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009.

Quick Quiz: Code Clichés

Name the two code clichés used in interactive calculator for motion:

(1) Sentinel guarded loop

(2) Enumeration

Page 14: Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009.

Demo 3:

File I/O

Page 15: Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009.

File Input and Output

Input

[cartypes velocities] = textread(input_filename, '%s %f', 'delimiter', '\t');

Output

open_output_file = fopen(output_filename, 'wt');

fprintf(open_output_file, '%s velocity is %f ms-1.\n', cartype, velocity);

fclose(open_output_file);

Page 16: Input and Output in MATLAB Lecture 13: Supporting Material Dr Kathryn Merrick Tuesday 14 th April, 2009.

Summary

After today’s lecture you should be able to:

Gather user input Format and display

output Input and output data

using files Design simple, text-

based user interfaces