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
0
votes
1 answer

Replicate elements of cell 1 to match length of cell 2

I tried to find an answer to this question in the advanced search but couldn't find one. I might have missed it, in which case, apologies. I guess it is a simple question, but i'm having trouble fixing it. I have a cell array, with each cell…
0
votes
1 answer

How to preallocate and populate cell array in Matlab

dataset=importdata(filename); [r,c]=size(dataset.data); names2=dataset.textdata(2:r+1, 1); names1=dataset.textdata(1, 2:c+1); rc=r*c; couples=cell(rc, 2); k=1 for i=1:r for j=1:c couples{k}=[names2(i), names1(j)]; k=k+1; end end The…
user3290180
  • 4,260
  • 9
  • 42
  • 77
0
votes
1 answer

Using fprintf for csv in matlab

I would like to generate a csv file with a header and some data. Since I am on a Mac, this does not work using csvwrite, so I need to use fprintf. I am storing my headers in a cell array called header (header = 'AB' 'CB'). Whatever I am trying I get…
C.Colden
  • 627
  • 1
  • 8
  • 28
0
votes
2 answers

Cell Array Manipulation

I have a number of cell arrays that have different formats. Some contain only numbers, some contain several numbers in each cell (each cell is a cell by itself), some contain words, and some contain several words in each cell (each cell is a cell by…
H_A
  • 667
  • 2
  • 6
  • 13
0
votes
1 answer

Extract data directly from several vectors within a cell array in Matlab

I have a structure like this one: >> A = [1 2; 3 4]; >> B = [5 6; 7 8]; >> C = [9 10; 11 12]; >> D = [13 14; 15 16]; >> S = {A B; C D} S = [2x2 double] [2x2 double] [2x2 double] [2x2 double] I wonder if there is any way to extract the…
baister
  • 283
  • 2
  • 9
0
votes
2 answers

Matlab: 2D cell array to char array

I was wondering if there is a nice way in which I can convert my 2D cell array into a 2D char matrix. The cell array looks like this: 'test' 'me' '0' '22' 'tester' 'me' '14' '241' 'test' 'you' '25' '1' And now I would…
Stefan
  • 414
  • 1
  • 6
  • 17
0
votes
0 answers

can't access string from a cell in matlab

info: C{1}(1) ans = '0.00000000 188000.00000000 ' class(C{1}(1)) ans = cell problem (when I am trying to access the first number in the form of string, it refuse to give me): C{1}(1)(1:10) Error: ()-indexing must appear last in an…
Suicide Bunny
  • 894
  • 1
  • 10
  • 23
0
votes
1 answer

Matlab: adding cell to cell array

Suppose I have a 3x1 cell array: c = {[1, 2, 3]; [1, 2, 3, 4, 5]; [1, 2]} I now want to add another array, to make it a 4x1 array. How do I do this? I have tried the following: c = {c; [1, 2, 3, 4]} But it then tells me that: c = {3x1 cell} …
Karnivaurus
  • 22,823
  • 57
  • 147
  • 247
0
votes
1 answer

Place equal elements in cell array

I have an array. I sorted it, so I have sorted array and indeces of sorted elements in the initial array. Fo example, from [4 5 4 4 4 4 5 4] I got [4 4 4 4 4 4 5 5] and [1 3 4 5 6 8 2 7]. How to place recieved indeces in a cell array, so that in…
Macaronnos
  • 647
  • 5
  • 14
0
votes
2 answers

How to add rows in between cell array in Matlab?

I have two cell arrays. A(290*6) and B(300*6); First column in two arrays are identical. I compared first column of two cell arrays using 'ismember'. I want to do that ; where cell elements are missing in cell array(A), I have to add a row where…
user3248216
  • 17
  • 2
  • 6
0
votes
1 answer

Mix letters of a string in alphabetical order in matlab

I have the cell array of strings in matlab. I want to sort letters in every string in alphabetical order. How can I do that? For example, if I have ['dcb','aetk','acb'}], I want it to be: ['bcd','aekt','abc'].
Macaronnos
  • 647
  • 5
  • 14
0
votes
1 answer

Dynamic struct fieldnames in Matlab - advice for beginner?

I have > 10 text files that I've read into a variable "files" using dir(*.txt). The files consist of matrices of varying dimensions on the order of 100x1000. The filenames for each are of the same format (L\d{1,2}N_20\d{2}.txt), so, with the…
k.mat27
  • 163
  • 2
  • 8
0
votes
1 answer

Searching a cell array of vectors and returning indices

I have a 3000x1 cell array of vectors of different lengths and am looking for a way to search them all for a number and return the cell indices for the first and last occurrence of that number. So my data looks like this: [1] [1 2] [1 2] [3] [6 7 8…
neptune36
  • 789
  • 1
  • 6
  • 10
0
votes
1 answer

Cells of different sizes - Overcoming exceed index error

I want to find the r^2 for each of the 3rd dimensions (the 3rd dimension is basically columns of data). However, in trying to index into each of the cells with a for loop (to loop through the states and then loop through the sets of data), I run…
SugaKookie
  • 780
  • 2
  • 17
  • 41
0
votes
3 answers

replacing values in a cell array for matching values from another

There is a cell array in MATLAB B = [ 1708] [ 2392] '+' [ 3394] [ 3660] '+' [ 5490] [ 5743] '+' [ 7555] [ 7809] '-' [ 9256] [ 9509] '-' [12878] [15066] '-' [16478] [17458] …
chris
  • 190
  • 1
  • 14