Problem 40 Define the vector
1.3 MORE COMPLICATED DATA STRUCTURES
1.3.4 Access to Cell Array Elements
[] [] [] [] {1x2 cell}
As you can see, in this example the first two measurements on day 1 will be stored and then the 5th measurement on day 3. The fields in between will be automatically created and filled with the empty field []. The individual elements in the cells are evidently, in turn, cell arrays. Calling cellplot
>> cellplot(meascampaign)
generates the overview of the structure of meascampaign shown in Fig. 1.20.
FIGURE 1.20 Visualization of the cell array measurements with cellplot.
1.3.4 Access to Cell Array Elements
Just as with the definition of cell arrays, access to the data in a cell array demands that attention be paid to the two different indexing schemes, cell indexing and content indexing.
In the above example, content indexing ({}) was used to gain access to the contents of the cells in the cell array measurements in order to store them in numerical variables:
>> position = measurements{1}
position =
256.9000 300.7000
>> value = measurements{2}
value =
10 27
50 16
100 5
On the other hand, with
>> cell1 = measurements(1)
cell1 =
[1x2 double]
>> cell2 = measurements(2)
cell2 =
[3x2 double]
cell indexing is used to gain access to the cells themselves (!), in order to store them as cells (!) in the new cell arrays cell1 and cell2, as a glance at the workspace shows:
>> whos
Name Size Bytes Class
cell1 1x1 76 cell array
cell2 1x1 108 cell array
meascampaign 3x5 828 cell array
measurements 1x2 184 cell array
position 1x2 16 double array
value 3x2 48 double array
Grand total is 79 elements using 1260 bytes
To get access to the data in the cell array meascampaign we have to gain access step-by-step with multiple indexing, since the cells of
meascampaign themselves contain cell arrays which, in turn, contain numerical arrays:
>> meas1day1 = meascampaign{1,1}
meas1day1 =
[1x2 double] [3x2 double]
>> iscell(meas1day1) %test: is it a cell array?
ans =
1
>> position1day1 = meascampaign{1,1}{1}
position1day1 =
256.9000 300.7000
>> measpos1day1 = meascampaign{1,1}{2}
measpos1day1 =
10 27
50 16
100 5
>> meas3pos1day1 = meascampaign{1,1}{2}(3,:)
meas3pos1day1 =
100 5
>> tempmeas3pos1day1 = meascampaign{1,1}{2}(3,2)
tempmeas3pos1day1 =
5
In order to understand what happens, let us take a look at the last instruc-tion as an example. With meascampaign{1,1} the contents of the cell
with the coordinates (1, 1) are reached. This is, again, a cell array, whose second cell component is reached with {2}. This second cell component contains, in turn, a 3× 2 matrix. The second component in the third row of this matrix is specified by (3,2), in the conventional indexing for numerical fields.
Modifying Cell Arrays
As already noted, caution is called for when redefining cell arrays if a variable with the same name already exists. For example,
>> measurements = [200, 30]
measurements =
200 30
yields no error message—the existing cell array will simply be overwritten—
but
>> measurements{1} = [256.9, 300.7]
??? Cell contents assignment to a non-cell array object.
yields an error message, since the (numerical) field measurements already existed.
Individual components of a cell array can, of course, also be changed using the mechanisms described above. Thus, for example, with
>> meascampaign{1,1}{2}(3,:) = []
meascampaign =
{1x2 cell} {1x2 cell} [] [] []
[] [] [] [] []
[] [] [] [] {1x2 cell}
the last measurement on the 5th day will erased; but this cannot be recognized initially in the MATLAB answer since the whole cell array is returned. The instruction
>> celldisp(meascampaign)
meascampaign{1,1}{1} =
256.9000 300.7000
meascampaign{1,1}{2} =
10 27
50 16
...
shows, however, that the originally defined row 100, 5 from meascampaign{1,1}is absent.
PROBLEMS
Work through the following problems on the topic of cell arrays.
NOTE Solutions to all problems can be found in Chapter 4.
Problem 52
Change all the measured values to [-1,0] in the cell array meascampaign in the cell with coordinates (3, 5).
Problem 53
Extend the cell array measurements by one cell in which the name of the engineer who made the measurement is entered.
Then generate a 5× 1 cell array weeklymeasurements. Fill each cell with the cell array measurements. Then, for each day of the week enter an (arbitrary) name in the prescribed location.
Problem 54
Make the following changes in the cell array weeklymeasurements defined in Problem 53:
1. Change the name of the engineer who made the measurement on the 5th day to A. E. Neumann.
2. Add a measurement with the values 250,0 to the 1st day.
3. Save the data from the 3rd day in a separate cell array day3.
4. Using the function deal, whose syntax can be found under help, save the measurements for all five days in separate variables, monday, tuesday, wednesday, thursday, and friday.
The next problem introduces another useful I/O function, textscan (see Section 1.2.6). It is noteworthy in that it makes use of cell arrays. The
command textscan can read formatted data from a text file opened with the function fopen and arranges the results of the reading operation in a cell array.
Familiarize yourself with the syntax of these functions and then work out the following.
Problem 55
ON THE CD Open the file sayings.txt in the accompanying software using fopen
and read the first seven strings, which are separated by spaces, using the function textscan into a cell array sayings. Then close the file using fclose. Next, display the result of this reading operation on the screen using celldisp and cellplot.