CHARACTERS AND STRINGS: Going Beyond Numbers - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

CHARACTERS AND STRINGS: Going Beyond Numbers

Description:

... 1 * CHARACTERS AND STRINGS: Going Beyond Numbers. June 7, 99 ... Sometimes we need control over individual digits. Take. n=2290 ... The i th signal is at ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 26
Provided by: ece12
Category:

less

Transcript and Presenter's Notes

Title: CHARACTERS AND STRINGS: Going Beyond Numbers


1
CHARACTERS AND STRINGS Going Beyond Numbers
  • June 7, 99

2
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

3
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

4
CHARACTER ARRAY
  • How does MATLAB define a character array?
  • Any expression enclosed inside single quotes is
    converted to a character array
  • uvillanova
  • u is defined as a 1x9 character array taking up
    18 bytes

5
OTHER FORMS
  • All the usual MATLAB conventions apply. Type
    these out and see
  • uvillanova,university is a 1x19 array
  • ucharlesrichard. Check size of u
  • uvillanovauniversity. Does this work? why

6
STRING COMPARISON
  • One of the key operations performed on character
    arrays is string comparison
  • Take
  • s1ece 2290
  • s2ece 4790
  • cstrcmp(s1,s2)
  • c0 means s1 and s2 are not equal. c1 means they
    are

7
PARTIAL COMPARISON
  • It is possible to do partial matching, say, the
    first 3 characters
  • cstrncomp(s1,s2,4)
  • The result of this comparison will be c1,
    indicating that the first 4 positions are the same

8
RELATIONAL OPERATORS
  • Relational operators (,lt, gt) can be applied
    to character arrays
  • s1james
  • s2jenny
  • s1s2 returns a binary array with 1s pointing
    to positions of equality
  • In one statement find out in how many positions
    do s1 and s2 differ?

9
SEARCHING FOR A STRING
  • We can find the occurrence of a string by using
    findstr
  • labelvillanova
  • positionfindstr(nova,label)
  • findstr points to the beginning position of the
    string. Here, position6

10
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

11
NUMBER/STRING CONVERSION
  • Sometimes we need control over individual digits
  • Take
  • n2290
  • mint2str (n)
  • As a result, m becomes a 1x4 array with m(1)2.
    You can set, for example, m(2)3

12
Explore!
  • Lets set n2290. If you type n, you will get
    2290 but this is not the same as two thousand two
    hundred 90.
  • Define m2409 and do nm. What do you get and
    why?

13
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?

14
Try it!
Takes input from keyboard
Predefined function in MATLAB
Convert xmax to string
Form a long string to go in the title command
15
MIXING STRINGS AND NUMBERS
  • Lets say we want to create a text string that
    says
  • temperature plot4pm to 6pm
  • Call the string s
  • stemperature plot,int2str(4),pm to
    ,int2str(6),pm

16
TITLING A PLOT TRY IT
  • The following code inserts a parameter,
    determined at runtime, in a graph title
  • for i14
  • fi pick a frequency
  • xcos(2pift) signal to plot
  • figure(i)number figures
  • plot(t,x)grid
  • s'a sinusoid of frequency ',int2str(f),'
    Hz'construct title
  • title(s)
  • end

17
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

18
ILLUSTRATING WHAT WE WANT
  • Say we want to generate 10 sinusoids each at a
    different frequency then save them to individual
    files
  • t00.011
  • for f110
  • xcos(2pitf)
  • save datafile x
  • end
  • What we really want save statement to do is
  • save datafile1 x
  • save datafile2 x
  • save datafile3 x
  • ...

19
SOLUTIONeval
  • 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)

20
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

21
ANATOMY OF A STRING
  • Here is what you need to do
  • ssave datafile,int2str(i), x
  • then do eval(s)

blank
digit to be appended to file name
22
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?

23
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?

24
COMPOSING THE LOAD STRING
  • To load file hundreds of image frames, here is
    the code we can use

25
HOMEWORK
  • Generate 5 sinusoidal functions. The i th signal
    is at frequency 10i 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
Write a Comment
User Comments (0)
About PowerShow.com