Search code examples
matlabcell

Extracting data from a cell in Matlab


Suppose I have C cell as I mentioned below:

C =

[31, 17]      [57, 17]       [83, 17]      [109, 17]     [135, 17]
[31, 33]      [57, 33]       [83, 33]      [109, 33]     [135, 33]
[31, 49]      [57, 49]       [83, 49]      [109, 49]     [135, 49]
[31, 65]      [57, 65]       [83, 65]      [109, 65]     [135, 65]
[31, 81]      [57, 81]       [83, 81]      [109, 81]     [135, 81]
[31, 97]      [57, 97]       [83, 97]      [109, 97]     [135, 97]
[31, 113]     [57, 113]      [83, 113]     [109, 113]    [135, 113]
[31, 129]     [57, 129]      [83, 129]     [109, 129]    [135, 129]
[31, 145]     [57, 145]      [83, 145]     [109, 145]    [135, 145]
[31, 161]     [57, 161]      [83, 161]     [109, 161]    [135, 161]
[31, 177]     [57, 177]      [83, 177]     [109, 177]    [135, 177]

Now I want to extracting all of its data in new matrix-es
like this:

C11 = ([31,17] [57,17]; [31,33] [57,33])          
C12 = ([57,17] [83,17]; [57,33] [83,33])                       
C13 = ([83,17] [109,17]; [83,33] [109,33])    
C14 = ([109,17] [135,17]; [109,33] [135,33])    
C21 = ([31,33] [57,33]; [31,49] [57,49])                           
C22 = ([57,33] [83,33]; [57,49] [83,49])
C23 = ([83,33] [109,33]; [83,49] [109,49]) 

....... ........ ....... ...... ..               

C104 = ([109,161] [135,161]; [109,177] [109,177])

How can do that in Matlab?


Solution

  • Just use array subscripting with round parentheses. For instance ,C12 would be computed like so:

    >> C12(1:2, 2:3)
    
    ans =
        { [ 57 17 ]    [ 83 17 ] }
        { [ 57 33 ]    [ 83 33 ] }
    

    You can do a for loop to iterate over your cell array and extract the necessary cells one by one, and store each one in a different variable. However, all the data is already stored C, so why duplicate it? I suggest that you keep everything in a cell array and extract matrices only when you need them.