Search code examples
matlabplotmatlab-figurescatter-plotcell-array

Plotting a cell array


I need to plot a cell array with the following format in Matlab:

{[vector1], [vector2], ...}

Into a 2D graph with the index of the vector as the y and the vector as the x

([vector1], 1), ([vector2], 2), ...

Solution

  • Here's a simple option:

    % some arbitrary data:
    CellData = {rand(10,1)*50,rand(10,1)*50,rand(10,1)*50};
    
    % Define x and y:
    x = cell2mat(CellData);
    y = ones(size(x,1),1)*(1:size(x,2));
    
    % plot:
    plot(x,y,'o')
    ylim([0 size(x,2)+1])
    

    so you plot each vector of x on a separate y value:

    A cell plot

    It will work as long as your cell array is just a list of vectors.

    EDIT: For non equal vectors

    You'll have to use a for loop with hold:

    % some arbitrary data:
    CellData = {rand(5,1)*50,rand(6,1)*50,rand(7,1)*50,rand(8,1)*50,rand(9,1)*50};
    
    figure;
    hold on
    for ii = 1:length(CellData)
        x = CellData{ii};
        y = ones(size(x,1),1)*ii;
        plot(x,y,'o')
    end
    ylim([0 ii+1])
    hold off
    

    Cell plot 2

    Hope this answers your question ;)