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
108
votes
8 answers

How to search for a string in cell array in MATLAB?

Let's say I have the cell array strs = {'HA' 'KU' 'LA' 'MA' 'TATA'} What should I do if I want to find the index of 'KU'?
Benjamin
  • 1,223
  • 2
  • 9
  • 4
36
votes
2 answers

How do I detect empty cells in a cell array?

How do I detect empty cells in a cell array? I know the command to remove the empty cell is a(1) = [], but I can't seem to get MATLAB to automatically detect which cells are empty. Background: I preallocated a cell array using a=cell(1,53). Then…
N.C. Rolly
  • 363
  • 1
  • 3
  • 4
30
votes
2 answers

Difference between accessing cell elements using curly braces and parentheses

What is the difference between accessing elements in a cell array using parentheses () and curly braces {}? For example, I tried to use cell{4} = [] and cell(4) = []. In the first case it sets the 4th element to [], but in the second case it wiped…
rajan sthapit
  • 4,194
  • 10
  • 42
  • 66
24
votes
4 answers

How to replace all "string" type by "char" type in a cell array?

Context In R2016b, MATLAB introduced a new string datatype, in addition to the usual char datatype. So far, so good, but it is now giving me a lot of issues with the JSONlab toolbox I'm using. For instance, in R2015b, loadjson returns a 1x3 cell…
CitizenInsane
  • 4,755
  • 1
  • 25
  • 56
24
votes
2 answers

Add a new element to the end of an existing cell array

As the title already mentions, how is it possible to add a new cell array 1x1 at the end of an existing cell array, let's call him Q, which is a cell array 1x3256?
nik-OS
  • 327
  • 1
  • 4
  • 14
22
votes
4 answers

How do you sort and efficiently find elements in a cell array (of strings) in Octave?

Is there built-in functionality for this?
B Seven
  • 44,484
  • 66
  • 240
  • 385
18
votes
3 answers

Why is a trailing comma in a cell array valid Matlab syntax?

I was surprised today to discover that A = {1,2,3} and B = {1,2,3,} are both valid syntax in MATLAB. I would have expected the second statement to yield an error. As best as I can tell, they produce identical cell arrays (all([A{:}]==[B{:}])…
chessofnerd
  • 1,219
  • 1
  • 20
  • 40
18
votes
2 answers

Replace empty cells with logical 0's before cell2mat in MATLAB

I have an array of empty cells and ones that I want to convert to a logical array, where the empty cells are zeros. When I use cell2mat, the empty cells are ignored, and I end up with a matrix of solely 1's, with no reference to the previous index…
Doresoom
  • 7,398
  • 14
  • 47
  • 61
16
votes
2 answers

Is wrapping the function with the {} operator a valid replacement of 'UniformOutput', false in cellfun?

I am using cellfun to apply a function to each cell in a cell array. I know that I must set 'UniformOutput' to false whenever the function returns non-scalar values, so that the outputs of the function are returned encapsulated in a cell array. Take…
codeaviator
  • 2,545
  • 17
  • 42
15
votes
2 answers

"Flattening" a cell array

I have created a function which takes vectors for input variables and returns a cell array for each set of inputs. The final output variable (out) seems to consist of a 2x1 cell containing two 1x5 cells. I have provided a screenshot of this…
GloveTree
  • 371
  • 1
  • 6
  • 17
14
votes
1 answer

A cell array inside a struct in Matlab - possible?

I wanted to wrap up a few variables inside a single struct, for easier input and output from functions as they are sent around quite a bit. The problem is that one of the variables is a cell array - specifically containing strings. Evidently once…
dan12345
  • 1,594
  • 4
  • 20
  • 30
14
votes
1 answer

What is the equivalent to a Matlab cell array?

I am new to Python and trying to create something equivalent to Matlab's "cell array". Lets say I have 100 customers index 'C001', 'C002' etc. and I have different data for each customer: Size of premises in square meters [real number] categorical…
ikonikon
  • 275
  • 1
  • 2
  • 10
14
votes
1 answer

strsplit: undefined function for input type 'char'

I have a <20x1> cell array and each of them stores some data in the form of a string (as it appears to me!!!). I want to access each element of the cell as an individual string and split is in words. The cell array I have is <20x1> cell array and to…
BajajG
  • 2,134
  • 2
  • 25
  • 32
11
votes
3 answers

How to create cell-array in MATLAB and initialize all elements to the same object?

I have a matrix (call it X) that is initialized to say zero(3). I want to change the code so that X is a cell array of size (say) (3,1) and initialize each element to zero(3). I can do it with a loop but is there a better way? X = cell(3,1); for…
s5s
  • 11,159
  • 21
  • 74
  • 121
11
votes
2 answers

Generalization of mat2str to cell arrays

I sometimes miss a function to produce a string representation of a (possibly nested) cell array. It would be a generalization of mat2str, which only works for non-cell arrays (of numeric, char or logical type). Given an array x, how to obtain a…
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
1
2 3
68 69