Search code examples
matlabplotcell-array

subplots for a combination of cell arrays


I am attempting to generate a figure with several subplots, e.g.

time = 1:365;
data = {rand(365,1),rand(365,1),rand(365,1)};
data2 = {rand(365,1),rand(365,1),rand(365,1)};

figure(1);
for i = 1:length(data);
    for ii = 1:2:2*length(data);
        for jj = 2:2:2*length(data);
            subplot(5,2,ii);
            plot(time,data{i});

            subplot(5,2,jj);
            plot(time,data2{i});
        end
    end
end

From this code I was trying to generate a subplot for each cell in 'data' and 'data2' where each of the cells in 'data' were plotted in subplots 1,3,5 and those in 'data2' in subplots 2,4,6. The code that I generate reproduces the same figure in all of the subplots for data and data2 instead of what I described above. I'm guessing the problem here is that the number of cells in each data set is 3 and the loop runs over 6 iterations? How can i fix this?


Solution

  • Try this. You don't need this nested loop there...

    figure(1);
    for i = 1:length(data);
        subplot(5,2,(i-1)*2+1);
        plot(time,data{i});
    
        subplot(5,2,(i-1)*2+2);
        plot(time,data2{i});
    end