Matlab Tutorial 3: Strings in Matlab


There are lots of conversions in Matlab. In the table below we show some of them:

  • int2str(n) Converts an integer n to a string
  • hex2num(hstr) Converts hexadecimal number hstr to a float.
  • hex2dec(hstr) Converts hexadecimal string to decimal integer.
  • dec2hex(n) Converts decimal integer to hexadecimal string.
  • dec2base(n,base) Converts decimal integer to base B string.
  • bin2dec(str) Converts binary string to decimal integer.
  • dec2bin(n) Converts decimal integer to a binary string.
  • mat2str(A,n) Convert a 2-D matrix to a string in MATLAB syntax.

There are of course many more different conversions, but we will stop there and try a few of them:

>> a=1; % the class is double and the storage size is 8 bytes.
>> words='This is a string'

For those of you who are not familiar with hexadecimal numbers: it simply means that the base is 16, and the representation uses the numbers 0,1,2…9, A,B…..F and not 0, 1, 2…..9, 10,11,…15. Also try to go back from a hexadecimal representation to decimal representation. Don’t forget that the argument is a string. What becomes dec2base(4,3)?

>> words(2) % gives the second element in the variable words.
ans = h

There are logical functions for strings and functions to pick substrings. We have functions to add blanks and subtract characters. We can also alter from small to capital letters and vice versa. We can also decide if we have letters in a string. Comparison of strings are also possible and to decide if they are equal and many other things. I choose to exemplify the following:

  • blanks(n) Gives a string with n blanks
  • deblank(str) Subtracts all blanks at the end of the string.
  • lower(str) All letters are changed to small.
  • upper(str) All letters are changed to capital.
  • ischar(str) If string contains character => gives 1 in return, 0 otherwise.
  • isletter(str(i)) If element number i in the string is a letter=> gives one in return.
  • isspace(str) True for white space characters.
  • strcmp(str1,str2) returns 1 if strings S1 and S2 are the same and 0 otherwise.
  • strcmpi(str1,str2) returns 1 if strings S1 and S2 are the same except forcase and 0 otherwise.
  • strfind(str1,str2) returns the starting indices of any occurrences of the string str2 in the string str1.
  • findstr(str1,str2) returns the starting indices of any occurrences of the shorter of the two strings in the longer.

Introduce a string

>> words(2)='t' % replaces the second element in the vector with t.

and yet another string

>> str2='What’s your name ?' % Without any blanks.

Try some of the commands of the previous list. Especially the commands deblank, lower, upper, ischar, isletter and isspace with the argument str1. Try:

>>isletter(str1(3))
>>isletter(str1(4))

Also try to compare strings. What becomes the following:

>> strcmpi(deblank(str1),str2) % deblank(str1), creates a new string.

Pages: 1 2 3 4 5

3 Comments

  1. Khashabi — May 19, 2010 @ 6:23 pm

    great !
    go on …

  2. Khashabi — May 19, 2010 @ 7:20 pm

    seems missing a ‘\’ before ‘n’ (new line escape character)
    Line: x= input(‘Give me a name !: n’,’s’); % skips a row

  3. saravanan — November 21, 2011 @ 9:52 am

    You are saying it is possible to change a letter by assigning with new string. Is it possible in structures, for example,

    s.text = ‘TITLE=”PhotoStudio Exported Data”‘
    [1×98 char]
    ‘ZONE T=”PhotoStudio Data” K = 147 F=PAINT’

    OLD STRING
    s.text(3) = ‘ZONE T=”PhotoStudio Data” K = 169 F=PAINT’
    For REPLACING THE STRING, suppose if I assign
    s.text(3) = ‘ZONE T=”PhotoStudio Data” K = 13 L =13 F=PAINT’
    it is not assigning so and what is the problem in it?

RSS feed for comments on this post. TrackBack URI

Leave a comment