Matlab Tutorial 3: Strings in Matlab


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

We can also introduce the escape character sequences n for the command input. This can be written as:

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

Read a matrix from the command window and upon return display it. Use input and disp. It is no difference from reading a number.

>> A=input('Give me a matrixn'); disp(A)

We shall now look on another example where we have a interaction with the command window. Now we will use a trigonometric function as a string input. The m-file looks like below:

1
2
3
4
5
6
7
% The m-file was created by MatlabCorner.com.
% The user can choose between three trigonometric functions and
% have these plotted.
% The command fplot have two input arguments. The first one is a
% string and the second one is a vector that gives the plot interval.
fcn= input('Choose one of the functions: sin, cos or tan: ', 's')
fplot(fcn,[0 40])

We stated earlier that a string of characters is a row vector and this is nothing than a special case of a matrix, so we can create matrices consisting of strings, but note that these strings must have the same lengths. Otherwise each element in the matrix will differ and we can’t perform the basic operations such as addition, subtraction and so on. Now we present some typical names as strings:

>> S1='Jannis   ' % 8 characters, two are blank.
>> S2='John    ' % 8 characters, four are blank.
>> S3='Tatiana ' % 8 characters, one is blank.
>> S4='Jenna   ' % 8 characters, three are blank.
>> A=[S1 S2 S3 S4] % a matrix A, its elements are strings.

It is possible to assemble strings of characters in many different ways, but sometimes you would also like to have numbers inserted in the strings. This can be arranged by using conversions. We can now try the command num2str. The command converts a numerical value to a string.

>> x=num2str(123.4567,3) % converts the number to a string 
% with three characters.
>> u=['The number is ', x, ', who would have guessed !'];
disp(u)

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