0% found this document useful (0 votes)
10 views24 pages

Materials Files-642917 Lecture8Inputandoutput

This lecture covers the use of strings in MATLAB, including how to prompt users for input and create formatted output using disp and fprintf. Students will learn to manipulate strings, convert numbers to strings, and handle graphical input. The session also introduces data import techniques and prepares students for upcoming topics on logical comparisons and decision-making structures.

Uploaded by

gitanxtreme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views24 pages

Materials Files-642917 Lecture8Inputandoutput

This lecture covers the use of strings in MATLAB, including how to prompt users for input and create formatted output using disp and fprintf. Students will learn to manipulate strings, convert numbers to strings, and handle graphical input. The session also introduces data import techniques and prepares students for upcoming topics on logical comparisons and decision-making structures.

Uploaded by

gitanxtreme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

ENGR 108

Introduction to Computing for


Engineers and Scientists
LECTURE 8: INPUT AND OUTPUT

1
Objectives

After this lecture, students will be able to:


◦ Use a new data type, the string
◦ Prompt the user for text and graphical input
◦ Create output with disp and fprintf

2
Strings

Strings are special types of


arrays, with one row and a
column for every character in the
string. They are used for
representing text data.
We can modify and combine
strings the same way we do with
regular matrices.

3
Strings
>> n = 'Raritan Valley' >> a = 'apple';
n = >> b = 'banana';
Raritan Valley >> c = 'canteloupe';

>> n(1:7) >> [a, b, c]


ans = ans =
Raritan applebananacanteloupe

>> n(9:end) >> [a, ' ', b, ' ', c]


ans = ans =
Valley apple banana canteloupe

4
Strings and numbers
MATLAB doesn't allow you to mix numbers and strings without
converting the numbers to a string first. num2str(x) converts the
number(s) in x to a string.

>> x = pi;
>> disp(['Pi is approximately ', num2str(x)])
Pi is approximately 3.1416

>> y = [2, 3, 5];


>> disp([num2str(y), ' are three prime numbers'])
2 3 5 are three prime numbers

5
The input command

So far, every script we've made has done calculations on pre-provided


data written into the script commands. Sometimes we want to ask a
user for input, instead.
x = input(‘question’) displays the prompt ‘question' in the
Command Window instructing the user to enter a value, which will then
be stored in the variable x

6
The input command

7
The input command

8
The input command
MATLAB will automatically detect the kind of input based on how it is
formatted:
◦ Scalars – a single number
◦ Matrices – a list of numbers in square brackets
◦ Strings – characters in single quotes
◦ An expression to calculate – characters not in quotes

If you know the input needs to be a string, change the input


command to x = input('prompt', 's')
◦ With the 's' option, the input will not need to be in quotes.

9
Graphical input

[x, y] = ginput(n) stores


the x and y coordinates of the
next n points clicked in the
current figure window.

10
Output
>> x = magic(3);
>> disp(x)
8 1 6
3 5 7
disp(x) displays the contents
of the variable x. 4 9 2

>> y = 'Raritan Valley';


>> disp(y)
Raritan Valley

11
Formatted output
Sometimes the output from disp isn't adequate, or you want to display
numbers in a specific format.
fprintf (formatted print function) allows you to control the
formatting of numbers in output and automatically print multiple lines
of output.
fprint('format string', var1, var2...)
The format string is a template that describes how the output will
look, using placeholders that MATLAB will fill with the variables.

12
Formatted output

>> x = 1.2345;
>> fprintf('The answer is %f\n', x)
The answer is 1.234500
%f is a placeholder. MATLAB puts the contents of x where %f is in the
string.
\n tells MATLAB to move to the next line after printing the output. You
should always finish the format string with \n, or else all the output will
run together on the same line.

13
Formatted output

The format string can have more than one placeholder.


>> x = 3;
>> y = x^2;
>> fprintf('%d squared is %d', x, y)
3 squared is 9
MATLAB fills the first placeholder with the first variable, then fills the
remaining one with the second variable

14
Formatted output
If there are more variables than placeholders, MATLAB will re-use the
format string until all the variables have been used.

>> a = 'apple'; b = 'banana'; c = 'cantaloupe';


>> fprintf('I don''t want a %s\n', a, b, c)
I don't want a apple
I don't want a banana
I don't want a cantaloupe

If you need to use an apostrophe in a string, enter two single quotes.


Otherwise, MATLAB will think the string ends at the apostrophe.

15
Formatted output
If the provided variable is a matrix, MATLAB will print a line of output
for every entry in the matrix, starting in the upper left and going down
the columns.
>> x = 5:-1:1
x =
5 4 3 2 1
>> fprintf('T minus %d seconds\n', x)
T minus 5 seconds
T minus 4 seconds
T minus 3 seconds
T minus 2 seconds
T minus 1 seconds

16
Types of placeholders

17
Types of placeholders

18
Types of placeholders
You can specify the maximum length of the number and the number
of digits after the decimal point/number of significant figures:
◦ %8.2f is a placeholder up to 8 characters wide that will display a number
with 2 digits after the decimal point.
◦ %8.3g is a placeholder for a number in scientific notation 8 characters long,
with 3 significant figures.

The decimal point and the e+ in scientific notation count as characters.


If the number is shorter than the maximum width, spaces are put in
front of it to fill the full width. This is useful for making aligned tables.

19
Importing data

The Import Wizard (found in File > Import Data) is the easiest way to
retrieve data from external files.
Example file formats MATLAB can import:
◦ Text: .dat, .txt, .csv
◦ Spreadsheets: .xls, .xlsx, .wk1
◦ Images: .bmp, .jpg, .gif
◦ Sound: .wav
◦ Video: .avi, .mpg

20
Import Wizard

21
Import Wizard

22
Commands to import and
export data

For information about how to import and export specific types of files
within scripts or functions without using the Import Wizard, read help
fileformats to look up the correct command, then read the manual
entry on the command you want.

23
Next week

◦ Write commands that make relational and logical comparisons


◦ Use the find command to search through data for values that meet
specific constraints
◦ Use the if, else, elseif, switch/case structures to make decisions

24

You might also like