Matlab Tutorial 3: Strings in Matlab


>> D{1,1}=123.23
>> D{1,2}='kalle'
We can of course also make the whole cell array from the beginning.
>> A={'Charles', 'Anderson', 23, 'football';'Gwen','Nolan',34, 'golf'}
A =
'Charles' 'Anderson' [23] 'football'
'Gwen' 'Nolan' [34] 'golf'

Different applicable commands to cell arrays can be found in the table below:

  • cell(m,n) Creates an empty m by n cell array.
  • celldisp(A) Displays all elements in the cell array A.
  • cellplot(A) Gives a graphical picture of the cell array A.
  • cell2struct(A,post,dim) Creates a structure, if dim=1, the information is read column wise to field 1 in the structure.
>> cellplot(A)% the result can be seen in figure below
Charles Anderson 23 football
Gwen Nolan 34 golf

matlab string array Matlab Tutorial 3: Strings in Matlab

We will also exemplify how cell2struct can be used. If the concept structure is not known from programming you will also have a possibility later on in the course to get to know the concept. Structure is a variable containing different fields. Think of it as a method of organizing the variable. Each field can be accessed by dot notation.

>> E=cell2struct(A,{'first_name','last_name','age','sport'},2)
 
% notice that dimension is now 2.
E =
2x1 struct array with fields:
first_name
last_name
age
sport
Display the different fields within the structure:
E(1)
ans =
first_name: 'Charles'
last_name: 'Anderson'
age: 23
sport: 'football'
E(2)
ans=
first_name: 'Gwen'
last_name: 'Nolan'
age: 34
sport: ’golf’

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