# Plot a cell array containing n matrices on same graph but with different color for each matrix.

4 views (last 30 days)
Anuj Prajapati on 11 Aug 2017
Commented: Anuj Prajapati on 22 Aug 2017
I have a cell array with which contains n matrices. I want to plot the cell array with each matrix plotted on the same graph. Each matrix line should be plotted with a different color.
##### 2 CommentsShowHide 1 older comment
Anuj Prajapati on 16 Aug 2017
It's n-by-1 and is supposed to contain matrices of unequal dimensions.

Anuj Prajapati on 16 Aug 2017
This solves the problem. P{} contains the matrices.
colorspec = {[1 1 0]; [1 0 1]; [0 1 1];[1 0 0]; [0 1 0];[0 0 1];[0 0 0]};
figure
hold on;
for j = 1:n;
plot(P{j}, 'Color', colorspec{j})
end

Akira Agata on 17 Aug 2017
Here is another way. By using cellfun, you don't need to use for-loop.
% Sample 10-by-1 cell
C = cell(10,1);
for kk = 1:10
C{kk} = rand(randi(20),1);
end
% Plot
figure
hold on;
cellfun(@plot,C);

Cemil Közkurt on 13 Aug 2017
Edited: Cemil Közkurt on 13 Aug 2017
a=cell(2,2);
a{1,1}=[1:10;rand(1,10)];
a{1,2}=[1:5;rand(1,5)];
a{2,1}=[1:8;rand(1,8)];
a{2,2}=[1:3;rand(1,3)];
s=size(a);
for n=1:s(1)
for m=1:s(2)
plot(a{n,m}(1,:),a{n,m}(2,:))
hold on
end
end
Anuj Prajapati on 22 Aug 2017
Sorry I was using R2013a.