SUNY Geneseo Department of Mathematics
Monday, April 25
Math 230 02
Spring 2016
Prof. Doug Baldwin
>> % Create a cell array
>> pets = { 'Medea', 'cat', 16; 'Boris', 'cat', 3 }
pets =
'Medea' 'cat' [16]
'Boris' 'cat' [ 3]
>> % Pick a specific value out of it
>> pets{2,1}
ans =
Boris
>> % Pick a cell out of it
>> pets(2,1)
ans =
'Boris'
>> % Variations on cell and content indexing
>> pets{2,1}(1)
ans =
B
>> pets(1,:)
ans =
'Medea' 'cat' [16]
>> pets{1,:}
ans =
Medea
ans =
cat
ans =
16
>> [name,species,age] = pets{1,:}
name =
Medea
species =
cat
age =
16
>> species
species =
cat
>> % Add a new row to the cell array. Cell indexing because a row is a smaller cell array
>> pets(3,:) = {'Molly', 'horse', 7}
pets =
'Medea' 'cat' [16]
'Boris' 'cat' [ 3]
'Molly' 'horse' [ 7]
>> % Delete a row
>> pets(3,:) = []
pets =
'Medea' 'cat' [16]
'Boris' 'cat' [ 3]
>> % Change values
>> pets{1,3} = 234
pets =
'Medea' 'cat' [234]
'Boris' 'cat' [ 3]
>> pets{1,3}= '16'
pets =
'Medea' 'cat' '16'
'Boris' 'cat' [ 3]
>> % Swap rows
>> pets([1,2],:) = pets([2,1],:)
pets =
'Boris' 'cat' [ 3]
'Medea' 'cat' '16'