Sixth lecture

Contents

Strings

s1='This is a string'
s1 =

    'This is a string'

s2='this IS a string'
s2 =

    'this IS a string'

s3='Also a string'
s3 =

    'Also a string'

Strings are vectors, (you can ask the size of them, or index them). As vectors, you can't compare them if they have different length.

They are in the ASCII-table, you can ask they number, by converting them to double:

double(s1)
ans =

  Columns 1 through 13

    84   104   105   115    32   105   115    32    97    32   115   116   114

  Columns 14 through 16

   105   110   103

There are several builtin functions for handling string. For example this converts to lowercase.

lower(s1)
ans =

    'this is a string'

This comperes to strings, giving back a logical scalar.

strcmp(s1,s2)
ans =

  logical

   0

This comparison doesn't mind the lower case- upper-case differences:

strcmpi(s1,s2)
ans =

  logical

   1

You cant concatenate them:

[s1 s2]
ans =

    'This is a stringthis IS a string'

Convert string meaning numbers to their actual value as numbers.

s5='12'
a5=str2num(s5)
whos
s5 =

    '12'


a5 =

    12

  Name                 Size             Bytes  Class     Attributes

  a5                   1x1                  8  double              
  ans                  1x32                64  char                
  data1               13x15              1560  double              
  everything          13x16             24882  cell                
  i                    1x1                  2  char                
  numberofletter       1x26               208  double              
  numbers             13x15              1560  double              
  p                    1x1                  8  double              
  position             1x1                  8  double              
  s1                   1x16                32  char                
  s2                   1x16                32  char                
  s3                   1x13                26  char                
  s5                   1x2                  4  char                
  st                   1x527             1054  char                
  stl                  1x527             1054  char                
  texts               13x16             23562  cell                
  v                    1x1                  8  double              
  value                1x1                  8  double              

Cell matrices

With the help of cell matrices we can store different type of data in one matrix. We create it by telling its size:

data1=cell(2,3)
data1 =

  2×3 cell array

    {0×0 double}    {0×0 double}    {0×0 double}
    {0×0 double}    {0×0 double}    {0×0 double}

You have to use curly brackets for indexing:

data1{1,1}='Here'
data1 =

  2×3 cell array

    {'Here'    }    {0×0 double}    {0×0 double}
    {0×0 double}    {0×0 double}    {0×0 double}

data1{1,2}=12
data1 =

  2×3 cell array

    {'Here'    }    {[      12]}    {0×0 double}
    {0×0 double}    {0×0 double}    {0×0 double}

data1{1,3}=[2 4 1]
data1 =

  2×3 cell array

    {'Here'    }    {[      12]}    {1×3 double}
    {0×0 double}    {0×0 double}    {0×0 double}

This is not the elemenet of the cell matrix

data1(1,3)
ans =

  1×1 cell array

    {1×3 double}

Importing data from excel:

We can easily import data from excel files using the xlsread function. For example:

data1=xlsread('adatok.xls')
data1 =

  Columns 1 through 7

    1.0000    2.0000    3.0000    4.0000    0.3000    5.0000    6.0000
   10.0000   10.0000       NaN   10.0000       NaN       NaN    9.5000
   10.0000   10.0000       NaN       NaN       NaN         0    9.0000
   10.0000   10.0000   10.0000   10.0000       NaN    8.0000       NaN
   10.0000   10.0000    5.0000   10.0000       NaN   10.0000    2.0000
   10.0000    9.0000   10.0000   10.0000       NaN    8.0000    5.0000
   10.0000   10.0000   10.0000    4.0000       NaN    4.0000    2.0000
   10.0000       NaN   10.0000   10.0000       NaN    5.0000    8.0000
   10.0000    7.0000       NaN       NaN       NaN    5.0000       NaN
   10.0000    8.0000    6.0000   10.0000       NaN   10.0000   10.0000
   10.0000   10.0000       NaN   10.0000       NaN       NaN    9.5000
   10.0000   10.0000    5.0000   10.0000       NaN   10.0000    8.0000
   10.0000    9.0000    4.5000    7.0000       NaN    8.0000    9.0000

  Columns 8 through 14

    7.0000    8.0000    0.3000    9.0000   10.0000   11.0000   12.0000
       NaN       NaN       NaN       NaN       NaN       NaN       NaN
       NaN       NaN       NaN       NaN       NaN       NaN       NaN
       NaN       NaN       NaN       NaN       NaN       NaN       NaN
       NaN       NaN       NaN       NaN       NaN       NaN       NaN
       NaN       NaN       NaN       NaN       NaN       NaN       NaN
       NaN       NaN       NaN       NaN       NaN       NaN       NaN
       NaN       NaN       NaN       NaN       NaN       NaN       NaN
       NaN       NaN       NaN       NaN       NaN       NaN       NaN
       NaN       NaN       NaN       NaN       NaN       NaN       NaN
       NaN       NaN       NaN       NaN       NaN       NaN       NaN
       NaN       NaN       NaN       NaN       NaN       NaN       NaN
       NaN       NaN       NaN       NaN       NaN       NaN       NaN

  Column 15

       NaN
   39.5000
   29.0000
   48.0000
   47.0000
   52.0000
   40.0000
   43.0000
   22.0000
   54.0000
   39.5000
   53.0000
   47.5000

[numbers, texts, everything]=xlsread('adatok.xls');

Polimorphic functions

Built-in functions can be called different ways. For example:

rand
ans =

    0.1712

gives one random (?) number

rand(4)
ans =

    0.7060    0.0971    0.9502    0.7655
    0.0318    0.8235    0.0344    0.7952
    0.2769    0.6948    0.4387    0.1869
    0.0462    0.3171    0.3816    0.4898

gives a 4*4 matrix

rand(3,4)
ans =

    0.4456    0.7547    0.6551    0.4984
    0.6463    0.2760    0.1626    0.9597
    0.7094    0.6797    0.1190    0.3404

gives a 3*4 matrix.

This is a polymorphic function, that is it works even if it's called with different number of inputs, and the output depends on the number of inputs. We can write such functions with the help of the nargin function. The output of the nargin function is the number of input our function was called.

Let us make a multiplication table. If our function was called without an input, then the output should be a 10*10 table, if there was one input, then a squared table, if two inputs was given, say n an m, then a n*m sized table.

function out1=multiP(n,m)
 
if nargin==0 
    n=10;
    m=10;
elseif nargin==1
   m=n;
end

v1=1:n;
v2=1:m;
out1=v1'*v2;

Similarly to nargin, nargout gives us the number of outputs when the function was called.

min([1, 5 -1])
ans =

    -1

[p, v]=min([1, 5, -1])
p =

    -1


v =

     3

Recommended problems

1. problem: Consider the following text: A neuron or, also known as a neurone, is an electrically excitable cell that receives, processes, and transmits information through electrical and chemical signals. These signals between neurons occur via specialized connections called synapses. Neurons can connect to each other to form neural networks. Neurons are the primary components of the central nervous system, which includes the brain and spinal cord, and of the peripheral nervous system, which comprises the autonomic nervous system and the somatic nervous system.

Create a variable called st, which contains this text as a string. Find out

2. problem Make a function called ceasarS(s), which takes a string as an input. The output is a string, where every letter is shifted by one in the abc, expect for z, which is changed to a. Also write the decoding function.

3. problem: Write a function called findOut(s), which takes a string (which is the name of an excel file) as input. The outputs are the number of numbers in the excel file, and the minimum and maximum of them.

4. problem Write a function called swapP(v,a,b), which takes a v vector, and two doubles as input. The output is a modified v: every element in v, which equals to a should be exchanged by b. If the function was called swapP(v,a), then every element equals to a should be exchanged to 1, if it was called swapP(v), then every 0 should be changed to 1.