Matlab Tutorial 3: Strings in Matlab

Finally to examine the last to commands in the table.

>> a=1; % the class is double and the storage size is 8 bytes.
>> words='This is a string'
>> words(2) % gives the second element in the variable words.
ans = h

Exercise 2: Eval function

  • eval(str) Performs the command in the string str.
  • eval(str,alt) As above, but eval has an alternative alt if the first string str does not work.

In the m-file below there is an example of how to use the command eval. The m-file also contains an repetition consisting of a for-loop. It is executed for n=1,2,3 and 4 and then the program is finished. Execute the program to find out how it works.

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

Please notice that the Matlab command num2str is inside a string vector and evaluated with the Matlab command eval. We have in the two previous Matlab tutorials studied the concept function and used it to solve some simple problems. The function is called with or without several input arguments, but there are of course other possibilities to create functions. We will create an inline function that can be used in a similar way as the traditional function m-file.

g=inline(expr, arg1,arg2) Creates an inline function g from the expression expr that can be called with the variables x and y. Let’s illustrate this with an example:

>> g=inline('3*sin(x)+cos(y)','x','y') % definition of the function.
g =
Inline function:
g(x,y) = 3*sin(x)+cos(y)

Now we can use this function simply by writing :

>>g(pi, 2*pi) % x=pi and y=2*pi

We can also evaluate the function g for different x-y pairs and get the function plotted.

>> x=0:10; y=0:10; % contains all integer values from 0 to 10.
>> g(x,y)% results in a vector with 11 elements.

Exercise 3: Evaluating functions in a loop

Write an m-file that evaluates a function 3*sin(x)+2*cos(x)+tan(x) every tenth degree between 0 and 360. Plot the function versus x with suitable title and labels. Display the calculated values in a matrix according to: function values and x in two columns.

Exercise 4: Matrices including elements from different classes

We have earlier used matrices containing numerical elements or string of characters, but the elements must be of the same class, but matlab also permits other forms of matrices to exist with elements of different classes like cell arrays, but you can not perform the operations like multiplication, subtraction, addition and so on these. But remember that all rows and columns must have the same length as for an ordinary matrix. How how to create a cell array?

>> D=cell(3,4)% gives an empty cell array, with 3 rows and 4 columns.

Assignment can be achieved assigning each and every cell like:

Pages: 1 2 3 4 5


  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’

    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