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              
  adatok      2x3               398  cell                
  ans         1x32               64  char                
  s1          1x16               32  char                
  s2          1x16               32  char                
  s3          1x13               26  char                
  s5          1x2                 4  char                

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')
Error using xlsread (line 136)
XLSREAD unable to open file 'adatok.xls'.
File '/home/orsi/Dropbox/Órák/BevMatlabM/Órák/en06/adatok.xls' not found.

Error in labor07en (line 53)
data1=xlsread('adatok.xls')
[numbers, texts, everything]=xlsread('adatok.xls');

Polimorphic functions

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

rand

gives one random (?) number

rand(4)

gives a 4*4 matrix

rand(3,4)

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])
[p, v]=min([1, 5, -1])

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.