Embed
Email

characters

Document Sample

Shared by: yurtgc548
Categories
Tags
Stats
views:
0
posted:
1/22/2012
language:
pages:
25
June 7, „99





CHARACTERS AND STRINGS:

Going Beyond Numbers









1999 BG Mobasseri 1 1/22/2012

WHAT DO WE WANT TO DO?



 MATLAB is first and foremost a

“numerical” computation platform

 In practice, however, there are many

occasions where one has to work with

characters or alpha-numeric quantities







1999 BG Mobasseri 2 1/22/2012

EXAMPLES



 Here are a few examples:

– placing titles and labels on graphs

– working with file names such as day1.dat,

day2.dat, etc.

– comparing two strings, one user supplied

and one in the library

– decimal to binary string conversion





1999 BG Mobasseri 3 1/22/2012

CHARACTER ARRAY



 How does MATLAB define a character

array?

 Any expression enclosed inside single

quotes is converted to a character array

– u=„villanova‟

– u is defined as a 1x9 character array taking

up 18 bytes



1999 BG Mobasseri 4 1/22/2012

OTHER FORMS



 Allthe usual MATLAB conventions

apply. Type these out and see

– u=[„villanova‟,‟university‟] is a 1x19 array

– u=[„charles‟;‟richard‟]. Check size of u

– u=[„villanova‟;‟university‟]. Does this work?

why







1999 BG Mobasseri 5 1/22/2012

STRING COMPARISON



 One of the key operations performed on

character arrays is string comparison

 Take

– s1=„ece 2290‟

– s2=„ece 4790‟

– c=strcmp(s1,s2)

 c=0 means s1 and s2 are not equal. c=1

means they are

1999 BG Mobasseri 6 1/22/2012

PARTIAL COMPARISON



 It is possible to do partial matching, say,

the first 3 characters

– c=strncomp(s1,s2,4)

 The result of this comparison will be

c=1, indicating that the first 4 positions

are the same





1999 BG Mobasseri 7 1/22/2012

RELATIONAL OPERATORS



 Relational operators (==,=) can be

applied to character arrays

– s1=„james‟

– s2=„jenny‟

– s1==s2 returns a binary array with 1‟s

pointing to positions of equality

 In one statement find out in how many

positions do s1 and s2 differ?

1999 BG Mobasseri 8 1/22/2012

SEARCHING FOR A STRING



 We can find the occurrence of a string

by using findstr

– label=„villanova‟

– position=findstr(„nova‟,label)

 findstr points to the beginning position

of the string. Here, position=6





1999 BG Mobasseri 9 1/22/2012

REPLACING A STRING



 We can replace the occurrence of a

string using strrep

– strrep(label,‟vill‟,‟Vill‟)

 Try changing Bill Clinton to William

Clinton using strrep







1999 BG Mobasseri 10 1/22/2012

NUMBER/STRING CONVERSION



 Sometimes we need control over

individual digits

 Take

– n=2290

– m=int2str (n)

 As a result, m becomes a 1x4 array with

m(1)=2. You can set, for example,

m(2)=3

1999 BG Mobasseri 11 1/22/2012

Explore!



 Let‟sset n=„2290‟. If you type n, you will

get 2290 but this is not the same as two

thousand two hundred 90.

 Define m=„2409‟ and do n+m. What do

you get and why?







1999 BG Mobasseri 12 1/22/2012

LABELING PLOTS AT RUNTIME



 Sometimes plot labels are not known

before execution

 For example, plot title should say...

voltage ranges from 5 to 9 volts. The

problem is these numbers are

determined at runtime

 How do we pass 5 and 9 to the title

command at runtime?

1999 BG Mobasseri 13 1/22/2012

Takes input from keyboard Try it!

Predefined function in MATLAB









Convert xmax to string









Form a long string to go in the title command





1999 BG Mobasseri 14 1/22/2012

MIXING STRINGS AND NUMBERS



 Let‟s say we want to create a text string

that says

temperature plot:4pm to 6pm

 Call the string s

s=[„temperature plot:‟,int2str(4),‟pm to „,int2str(6),‟pm‟]









1999 BG Mobasseri 15 1/22/2012

TITLING A PLOT: TRY IT



 The following code inserts a parameter,

determined at runtime, in a graph title

for i=1:4

f=i;% pick a frequency

x=cos(2*pi*f*t); %signal to plot

figure(i);%number figures

plot(t,x);grid;

s=['a sinusoid of frequency ',int2str(f),' Hz'];%construct title

title(s)

end



1999 BG Mobasseri 16 1/22/2012

SAVING LARGE NUMBER OF FILES

AT ONCE

 In many situations your code generates

a large number of files which you want

them saved, each under a different

name, like

– datafile1.mat

– datafile2.mat

– etc



1999 BG Mobasseri 17 1/22/2012

ILLUSTRATING WHAT WE WANT



 Say we want to  What we really want

generate 10 sinusoids save statement to

each at a different

do is

frequency then save

them to individual files – save datafile1 x

– t=0:0.01:1; – save datafile2 x

– for f=1:10 – save datafile3 x

– x=cos(2*pi*t*f); – ...

– save datafile x

– end

1999 BG Mobasseri 18 1/22/2012

SOLUTION:eval



 eval(s) evaluates string s as if it was an

explicit MATLAB statement typed in the

command window

 For example,



eval(„plot(x)‟)

– is equivalent to plot(x)





1999 BG Mobasseri 19 1/22/2012

CUSTOMIZING STRING AT

RUNTIME

 First, put together the string piece by

piece. Here we want the filename

appended by the loop index, i.e.

datafile1, datafile2 etc

 Then insert the string inside eval









1999 BG Mobasseri 20 1/22/2012

ANATOMY OF A STRING



 Here is what you need to do blank







s=[„save datafile‟,int2str(i),‟ x‟]



 then do eval(s) digit to be appended

to file name





1999 BG Mobasseri 21 1/22/2012

LOADING MULTIPLE DATA FILES



 The inverse of saving many files at once

is loading them

 If you are given hundreds of files, how

would you read them?









1999 BG Mobasseri 22 1/22/2012

EXAMPLE: LOADING DIGITAL

VIDEO

 Video is a collection of thousands of still

frames that are saved in files named

frame1,frame2,frame3 etc.

 Question is how do you import them into

MATLAB frame by frame, but not

manually?





1999 BG Mobasseri 23 1/22/2012

COMPOSING THE LOAD STRING



 To load file hundreds of image frames,

here is the code we can use









1999 BG Mobasseri 24 1/22/2012

HOMEWORK



 Generate 5 sinusoidal functions. The i th

signal is at frequency 10*i Hz. Plot each in a

separate figure window with its title stating the

frequency of the sinusoid

 Save each sinusoid in a separate datafile

calling them sine1.dat, sine2.dat etc. Check

to see if the files were actually created







1999 BG Mobasseri 25 1/22/2012



Related docs
Other docs by yurtgc548
AC120 lecture 26
Views: 1  |  Downloads: 0
ABSTRACT - GPCET MCA EMERALDS
Views: 0  |  Downloads: 0
Absolute Garbage Systems
Views: 1  |  Downloads: 0
Abnormal Psychology
Views: 0  |  Downloads: 0
ABC of Arterial and venous Disease
Views: 0  |  Downloads: 0
Abacus Fund Management LLC Presentation
Views: 0  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!