Questions tagged [cell-array]

In MATLAB cell arrays are a built-in container class allowing for the storage of different types of data in each "cell" of the array.

A cell array is a data type with indexed data containers called cells, where each cell can contain any type of data. Cell arrays commonly contain either lists of text strings, combinations of text and numbers, or numeric arrays of different sizes.

You can refer to subsets of cells by enclosing indices in smooth parentheses, (). The cell contents can be accessed by indexing with curly braces, {}.

Examples:

>> ca = cell(2, 3);        % create 2-by-3 cell array with empty cells
>> ca{1, 1} = 3;           % store a double scalar in one cell
>> ca{1, 2} = rand(3, 4);  % store an entire matrix into a cell
>> ca{1, 3} = 'will you look at that, a string in a cell!';
>> ca{2, 1} = @mean;       % a function handle can also be stored in a cell
>> ca{2, 3} = uint32(45);  % store a uint32 scalar
>> ca(1, 1:3)   % Get a subset of cells (first row)

ans =

  1×3 cell array

    [3]    [3×4 double]    'will you look at that, a string in a cell!'

>> ca{1, 2}     % Get the contents of a cell

ans =

    0.8147    0.9134    0.2785    0.9649
    0.9058    0.6324    0.5469    0.1576
    0.1270    0.0975    0.9575    0.9706
1035 questions
10
votes
1 answer

How can I accumulate cells of different lengths into a matrix in MATLAB?

So, I have a cell-array of 1xN vectors of different lengths. I want to append them into a matrix so I can display them with imagesc. Obviously the matrix must be the width of the largest vector. My current code for this is below: tcell = {[1,2,3],…
JudoWill
  • 4,741
  • 2
  • 36
  • 48
10
votes
3 answers

How do I apply a function with multiple parameters using `cellfun` (MATLAB)?

Using cellfun, how do I apply a function to all the cells created by the mat2cell function? My function is defined in another file, here it is referred to by myFunc. This function takes two arguments, which should be a cell and an integer. e.g.…
petehallw
  • 1,014
  • 6
  • 21
  • 49
10
votes
1 answer

How to convert an {Mx1} cell array of {1xN cell} arrays into a {1xN} cell array of {Mx1 cell} arrays?

Suppose that C is a cell array with shape M × 1 (i.e., size(C) returns [M 1]), and that each element of C is in turn a cell array with shape 1 × N. I often want to convert such a cell array to a new cell array D having shape 1 × N, with elements…
kjo
  • 33,683
  • 52
  • 148
  • 265
10
votes
1 answer

How can I access all field elements of a structure array nested in a cell array in MATLAB?

Here's code that creates an example cell array for this question: mycell = cell([5,1]); for i = 1 : size(mycell) mystruct = struct(); mycell{i} = mystruct; mycell{i}.field1 = i; end I expected mycell{:}.field1 to do what I want, but it…
Chad
  • 1,434
  • 1
  • 15
  • 30
9
votes
6 answers

How to average over a cell-array of arrays?

I have a cell array c of equal-sized arrays, i.e. size(c{n}) = [ m l ... ] for any n. How can I get the mean values (averaging over the cell array index n) for all array elements in one sweep? I thought about using cell2mat and mean but the former…
Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221
9
votes
3 answers

Finding and filtering elements in a MATLAB cell array

I have a list (cell array) of elements with structs like this: mystruct = struct('x', 'foo', 'y', 'bar', 's', struct('text', 'Pickabo')); mylist = {mystruct }; Now I would like to filter mylist for all structs…
smichak
  • 4,716
  • 3
  • 35
  • 47
9
votes
2 answers

How to apply cellfun (or arrayfun or structfun) with constant extra input arguments?

I want to apply a function to each element of a cell array -- so I have cellfun for that. However, the function takes two extra arguments (a string and a vector), which I want to keep constant for all the elements of the cell array; i.e. I'd like to…
antony
  • 2,877
  • 4
  • 31
  • 43
9
votes
3 answers

Convert cell to double

>> C = [{1} {2} ; {'@CF'} {2}] C = [ 1] [2] '@CF' [2] >> whos C Name Size Bytes Class Attributes C 2x2 478 cell How can I convert C into double so that: >> C C = 1 2 NaN 2 I've…
user1532230
  • 157
  • 1
  • 2
  • 7
8
votes
3 answers

Find unique rows of a cell array considering all possible permutations on each row

I have cell array A of dimension m * k. I want to keep the rows of A unique up to an order of the k cells. The "tricky" part is "up to an order of the k cells": consider the k cells in the ith row of A, A(i,:); there could be a row j of A, A(j,:),…
TEX
  • 2,249
  • 20
  • 43
8
votes
3 answers

MATLAB: comparison of cell arrays of string

I have two cell arrays of strings, and I want to check if they contain the same strings (they do not have to be in the same order, nor do we know if they are of the same lengths). For example: a = {'2' '4' '1' '3'}; b = {'1' '2' '4' '3'}; or a =…
Dave
  • 629
  • 3
  • 7
  • 11
7
votes
4 answers

MATLAB "bug" (or really weird behavior) with structs and empty cell arrays

I have no idea what's going on here. I'm using R2006b. Any chance someone out there with a newer version could test to see if they get the same behavior, before I file a bug report? code: (bug1.m) function bug1 S =…
Jason S
  • 184,598
  • 164
  • 608
  • 970
7
votes
2 answers

How does one concatenate cell arrays that are part of a cell array in MATLAB?

I have a cell array allData which is Nx1. Each cell contains a structure with a names property (the name is a custom object, but think of it as a cell array of strings if you like). I would like to create a single cell array that contains all of the…
PengOne
  • 48,188
  • 17
  • 130
  • 149
7
votes
2 answers

How to remove zero entries inside a cell array in MATLAB?

I have a cell array in MATLAB, lets say cell_arr and it has zero entries as well as non-zeros cell entries. For example: cell_arr = {0, 0, 0, 0, 0, {1x3 cell}, {1x3 cell}, {1x3 cell}, {1x3 cell}}; Can somebody please tell how to remove these zero…
Sanchit
  • 3,180
  • 8
  • 37
  • 53
7
votes
4 answers

concatenate vectors of an cell array in matlab

In matlab I have a 4x5 cell array where each cell consists of an 121x1 vector. What is the easiest way to create an 3-dim 4x5x121 matrix avoiding a 2-fold loop.
user1618022
  • 163
  • 1
  • 6
7
votes
2 answers

Calling function with varying number of parameters in Matlab

I am using symbolic toolbox to generate a matlab function. But the number of input to the generated function is varying with the number of objects that I need (e.g., number of switches). For 2 and 3 switches the generated function look likes this…
Torstein I. Bø
  • 1,359
  • 4
  • 14
  • 33
1
2
3
68 69