Matlab Tutorial 3: Strings in Matlab


>> 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

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.
>> words(2)='t' % replaces the second element in the vector with t.

matlab-string-array

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’

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’

    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?