How do concatenation and indexing differ for cells and arrays in MATLAB?

I am a bit confused about using cells and arrays in MATLAB and would like some clarification on a few points. Here are my observations:

  • An array can dynamically tune its own memory to provide a dynamic number of elements, while cells do not seem to act the same:

    a=[]; a=[a 1]; b={}; b={b 1}; 
  • Several elements can be extracted from cells, but it looks like they cannot be from arrays:

     a={'1' '2'}; figure; plot(...); hold on; plot(...); legend(a{1:2}); b=['1' '2']; figure; plot(...); hold on; plot(...); legend(b(1:2)); %# b(1:2) is an array, not its elements, so it is wrong with legend. 

Are they right? What are some other uses between cells and an array?

+4
source share
2 answers

Array cells can be a little complicated, because you can use the syntax [] , () and {} in different ways to create , concatenate , and index them, although each of them does different things. The solution to two issues:

  • To grow an array of cells, you can use one of the following syntaxes:

     b = [b {1}]; % Make a cell with 1 in it, and append it to the existing % cell array b using [] b = {b{:} 1}; % Get the contents of the cell array as a comma-separated % list, then regroup them into a cell array along with a % new value 1 b{end+1} = 1; % Append a new cell to the end of b using {} b(end+1) = {1}; % Append a new cell to the end of b using () 
  • When you index an array of cells with () , it returns a subset of the cells in the array of cells. When you index an array of cells with {} , it returns a list, separated by commas, of the contents of the cell. For instance:

     b = {1 2 3 4 5}; % A 1-by-5 cell array c = b(2:4); % A 1-by-3 cell array, equivalent to {2 3 4} d = [b{2:4}]; % A 1-by-3 numeric array, equivalent to [2 3 4] 

    For d syntax {} extracts the contents of cells 2, 3, and 4 as a comma-separated list , then uses [] to collect these values ​​in a numeric array. Therefore, b{2:4} equivalent to writing b{2}, b{3}, b{4} or 2, 3, 4 .

    For your legend call, the syntax for legend(a{1:2}) equivalent to legend(a{1}, a{2}) or legend('1', '2') . Thus, two arguments (two separate characters) are passed to legend . The syntax legend(b(1:2)) passes a single argument, which is a 1 by 2 '12' string.

+12
source

Each array of cells is an array! From this answer :

[] is the operator associated with the array. An array can be of any type - an array of numbers, an array of char (string), an array of structures, or an array of cells. All elements of the array must be of the same type!

Example: [1,2,3,4]

{} is a type. Imagine that you want to put elements of various types into an array - a number and a string. This is possible with a trick - first put each element in the container {} , and then create an array with these containers - an array of cells.

Example: [{1},{'Hallo'}] with the abbreviation {1, 'Hallo'}

+4
source

Source: https://habr.com/ru/post/1307279/


All Articles