Lesson 5 - Text in Matlab
Lesson 5 - Text in Matlab
Forma+ed
text
EECS
1011
Session
5
1
Reference
} You
should
be
following
along
in
Ch.
2
&
Ch.
7
of
the
Attaway
book.
Ch.
7.
Manipulating
text
&
“strings”
in
MATLAB
2
http://site.ebrary.com.ezproxy.library.yorku.ca/lib/oculyork/detail.action?docID=10483436
Easiest
way
to
try
MATLAB?
WebFAS
} webfas.yorku.ca
(it’s
free
MATLAB
on
the
web!)
} Video
intro:
https://youtu.be/Kv9UVJTTb6E
3
Text
output
[Fill
in
here]
} a
MATLAB
string
is
a
row
vector
of
characters
} any
text
enclosed
by
single
quotes
is
considered
a
MATLAB
string
} the
string
is
actually
a
vector
that
contains
the
numeric
codes
for
the
characters
(codes
0
to
127
are
ASCII)
} the
length
of
s
is
the
number
of
characters
} a
quotation
within
the
string
is
indicated
by
two
quotation
marks
4
Text
output
[Fill
in
here]
} the
command
disp
will
display
a
MATLAB
string
to
the
screen
without
displaying
the
name
of
the
variable
>>
s =
Any characters
>>
Any characters
5
Forma+ed
output
} it
is
often
very
useful
to
generate
strings
programmatically
} display
output
more
elaborate
than
just
a
variable
value
} display
a
table
of
values
} export
data
to
a
non-‐native
MATLAB
format
} generate
the
string
for
a
MATLAB
command
} the
function
sprintf
is
used
to
generate
formatted
strings
} use
doc sprintf
to
get
help
for
sprintf
} help sprintf
is
less
useful
6
Forma+ed
output
[Fill
in
here]
} the
syntax
of
the
sprintf
command
is:
7
Forma+ed
output
} the
formatting
string
describes
how
MATLAB
should
convert
the
information
stored
in
the
arrays
into
text
} the
formatting
string
can
include:
} text
} escape
characters
} zero
or
more
percent
signs
each
followed
by:
} optional
operator
characters,
and
a
conversion
character
8
Forma+ed
output
} examples
using
only
plain
text
and
escape
characters
} see
textbook
or
doc sprintf
for
a
table
of
escape
characters
>> sprintf('hi')
>> sprintf('''hi''')
>> sprintf('hi\tbye')
>> sprintf('hi\nbye')
>> sprintf('50%%')
9
Forma+ed
output
[Fill
in
here]
} usually
you
will
need
to
include
a
conversion
character
and
an
array
containing
data
} the
conversion
character
tells
MATLAB
what
conversion
it
should
use
when
converting
the
data
in
the
variable
to
text
} most
commonly
used
conversions
are:
10
Forma+ed
output
[Fill
in
here]
} examples
using
simple
conversions
>> n = 10;
>> sprintf('There are items', )
11
Forma+ed
output
[Fill
in
here]
} for
floating-‐point
conversions,
you
can
specify
the
number
of
digits
after
the
decimal
place
} called
the
precision
} default
value
is
6
12
Forma+ed
output
[Fill
in
here]
} for
all
conversions,
you
can
specify
the
minimum
number
of
characters
to
output
>> n = 10;
>> sprintf('There are items', n)
13
Forma+ed
output
} there
are
many
other
options
in
sprintf
} see
doc sprintf
for
details
} experiment
the
options
14
Display
text
on
a
Graph
/
Figure
} On
edge
of
figure:
} Title
} X-‐axis
} Y-‐axis
} Inside
figure:
} Legend
} Anywhere
else.
http://www.youtube.com/watch?
v=FVZNCl9CT6I
15
16