Thursday, September 24, 2009

Matlab Plotting within a loop with different colors

Matlab by default has 7 colors. Following code demonstrates how one can invoke plot command within a for loop so that each time it plots a given data set in a different color. Don't worry about the variables that are used in this piece of code. You should notice that we can pass randomized color information to the plot command inside a for loop.


hold on;
for k = 1:length(idx)
ridx = find(idx == k);
color = rand(1,3);
for j = 1:length(ridx);
plot(logdata(:,1,ridx(j)),logdata(:,2,ridx(j)), ...
'Color',color,'LineWidth',2);
end
end
hold off;
 
pre { margin: 5px 20px; border: 1px dashed #666; padding: 5px; background: #f8f8f8; white-space: pre-wrap; /* css-3 */ white-space: -moz-pre-wrap; /* Mozilla, since 1999 */ white-space: -pre-wrap; /* Opera 4-6 */ white-space: -o-pre-wrap; /* Opera 7 */ word-wrap: break-word; /* Internet Explorer 5.5+ */ }