MATLAB: 2D matrix or cell with groups to compare – different length groups, unknown number of groups

cell arrays

I've been creating a file for analyzing some image data. I need to be able to edit it each time I use it. It will take groups of slides to compare to one another. The groups will often be different lengths and the number of groups will change. Also I would like other users to be able to be able to use the code without needing to understand too much about how it works. I know that cell arrays can take variable lengths in the cells so I was trying to use them but I can't seem to get it right.
Here's an example of what I might be trying to compare: If I have 8 images numbered 1,2….8 I make groups of images I want to compare:
group1 = [1 2 3 4]
group2 = [5 6 7 8]
group3 = [3 6]
then I put them in a cell array:
compareImages = {group1;group2;group3}
But I can't seem to access the individual elements in the groups.
printG1 = compareImages{1}
works but
printG2 = compareImages{2}{1}
Gives the error message: Cell contents reference from a non-cell array object.
I would prefer to initialize the cell array in a single line so the user doesn't have to add and remove groups but rather the whole thing would be something like this:
imageGroups = {1 2 3 4; 5 6 7 8; 3 6}
This seems to work well if the groups are all the same length but not when they have different numbers of elements.
Thank you for any help you can provide.

Best Answer

  • I figured out that if I did this:
    group1 = [1 2 3 4];
    group2 = [5 6 7 8];
    group3 = [3 6];
    A = num2cell(group1);
    B = num2cell(group2);
    C = num2cell(group3);
    imageGroups = {A;B;C};
    Then I could get an individual element using this:
    printG2 = imageGroups{2}{1}
    But it was still kind of awkward so I kept playing around until I found this:
    compareSlides = {{1 2 3}; {3 4}; {1 3}}; % initialize the whole array in a single line
    Which I could also access using:
    printG2 = imageGroups{2}{1}
    I hope someone else out there finds this useful.