Matlab Tutorial 3: Strings in Matlab


Finally to examine the last to commands in the table.

>>str3='peter'
>>strfind(str1,str3)
>>findstr(str1,str3)

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.

1
2
3
4
5
6
7
% The m-file was created by MatlabCorner.com.
% Magic(N) is a N-by-N matrix constructed from the integers
% 1 through N^2 with equal row, diagonal and column sums.
% Produces valid magic squares for all N> 0 except N=2.
for n=1:4
         eval(['M' num2str(n) '=magic(n)'])
end

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
ans=
1.0000

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:

Related posts:

  1. Matlab Tutorial 4: Data Analysis and Statistics with Matlab
  2. Matlab Tutorial 1: Hello world, plotting, mathematical functions and file types
  3. Matlab Tutorial 2: Matrices in Matlab
  4. Matlab Tutorial 7: Common Programming Structures and Conditional Statements

Pages: 1 2 3 4 5

2 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

RSS feed for comments on this post. TrackBack URI

Leave a comment