Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Одиноков / МиИПиС_асп_13г / MATLAB_R2013a_Выбор.doc
Скачиваний:
33
Добавлен:
15.04.2015
Размер:
4.03 Mб
Скачать
  •   R2013a>MATLAB>Language Fundamentals>Data Types>Cell Arrays

  • Multilevel Indexing to Access Parts of Cells

This example explains techniques for accessing data in arrays stored within cells of cell arrays. To run the code in this example, create a sample cell array:

myNum = [1, 2, 3];

myCell = {'one', 'two'};

myStruct.Field1 = ones(3);

myStruct.Field2 = 5*ones(5);

C = {myNum, 100*myNum;

myCell, myStruct};

Access the complete contents of a particular cell using curly braces, {}. For example,

C{1,2} % Содержимое ячейки (1,2) массива С.

returns the numeric vector from that cell:

ans =

100 200 300 (содержимое-числа ячейки (1,2) массива С; если содержимое велико, оно сокращается до отображения типа и структуры данных, например, ans=[200x200 double])

ДОПОЛНЕНИЕ. Команда

C(1,2)

дает ячейку (1,2) в массиве C:

ans =

[1x3 double] (указывается тип массива -числовой - и его структура - вектор-строка; не путать с сокращенным отображением данных) (Конец ДОПОЛНЕНИЯ)

Access part of the contents of a cell by appending indices, using syntax that matches the data type of the contents. For example:

  • Enclose numeric indices in curly braces (в оригинале - smooth parentheses). For example, C{1,1} returns the 1-by-3 numeric vector, [1, 2, 3] (точный MATLAB-ответ: ans = 1 2 3). Access the second element of that vector with the syntax

C{1,1}(1,2) % {1,1} - указание на содержимое ячейки (1,1) массива C; (1,2) - указание на элемент (1,2) в C{1,1}.

which returns

ans =

2 (элемент (1,2) массива C{1,1})

ДОПОЛНЕНИЕ. Команда

C{1,1}{1,2}

неверна, так как {1,2} указывает на содержимое ячейки (1,2), в то время как C{1,1} - числовой массив, не содержащий ячеек. (Конец

ДОПОЛНЕНИЯ)

  • Enclose cell array indices in smooth parentheses (в оригинале - curly braces). For example, C(2,1) returns the cell array {'one', 'two'}(точный MATLAB-ответ: ans = {1x2 cell} - массив-вектор-строка ячеек). Access the contents of the second cell within that cell array with the syntax

C{2,1}{1,2} % {2,1} - указание на содержимое ячейки (2,1) массива C; {1,2} - указание на содержимое ячейки (1,2) в C{2,1}.

which returns

ans =

two

ДОПОЛНЕНИЕ. Команда

C{2,1}(1,2) % (1,2) - указание на элемент (1,2) в C{2,1}.

дает ячейку (1,2) в C{2,1}:

ans =

'two' (Конец ДОПОЛНЕНИЯ)

  • Refer to fields of a struct array with dot notation, and index into the array as described for numeric and cell arrays. For example, C{2,2} returns a structure array (точный MATLAB-ответ - содержимое элемента (2,2) массива C, т.е. содержимое полей структуры myStruct:

ans =

Field1: [3x3 double]

Field2: [5x5 double]

где показано сокращеное указание на искомое содержимое (не путать с аналогичным по форме указанием типа и структуры массива в

MATLAB-ответе на запрос по ячейке)

where Field2 contains a 5-by-5 numeric array of fives. Access the element in the fifth row and first column of that field with the syntax

C{2,2}.Field2(5,1) % (5,1) - указание на элемент (5,1) массива данных поля Field2.

which returns

ans =

5 (ответ верен, потому что запрос C{2,2}.Field2(5,1) относился к элементу (5,1) содержимого [5x5 double] как сокращенного отображения действительного числового массива 5х5 в поле Field2 структуры myStruct)

ДОПОЛНЕНИЕ. Команда

C(2,2).Field2(5,1)

неверна, так как (5,1) - указание на элемент (5,1) массива данных поля Field2, в то время как (2,2)- указание на ячейку (т.е. на её тип и структуру - [1x2 struct] ), но не на её содержание. (Конец ДОПОЛНЕНИЯ)

You can nest any number of cell and structure arrays. For example, add nested cells and structures to C.

C{2,1}{2,2} = {pi, eps};

C{2,2}.Field3 = struct('NestedField1', rand(3), ...

'NestedField2', magic(4), ...

'NestedField3', {{'text'; 'more text'}} );

%{

s = struct(field,value) creates a structure array with the specified field and values. If value is a cell array, then s is a structure array with the same dimensions as value. Each element of s contains the corresponding element of value. For example, s = struct('f',{'a','b'}) returns s(1).f= 'a' and s(2).f = 'b'. Для сохранения структуры myStruct массив {'text'; 'more text'} помещен в одну ячейку-скаляр {{'text'; 'more text'}}. При этом C{2,2}.Field3.NestedField3 становится содержимым ячейки-скаляра в виде массива-вектора-столбца из двух ячеек.

%}

These assignment statements access parts of the new data:

copy_pi = C{2,1}{2,2}{1,1}

part_magic = C{2,2}.Field3.NestedField2(1:2,1:2)

nested_cell = C{2,2}.Field3.NestedField3{2,1} % {2,1} - указание на содержимое ячейки (2,1) массива C{2,2}.Field3.NestedField3.

MATLAB® displays:

copy_pi =

3.1416

part_magic =

16 2

5 11

nested_cell =

more text