MATLAB Answers

0

HELP with for loop plots and changing symbol styles

Asked by Lindsay Anderson on 2 Apr 2016
Latest activity Answered by dpb
on 2 Apr 2016
Accepted Answer by dpb
Hi all, I am trying to generate a plot that looks like the attachment (that I generated without using for loops), using the code pasted below. However there seems to be an error somewhere as all of my symbols are coming out as black dots (not coloured black, red, and blue). This is my first time trying to use for loops to create figures, so the issue is likey in my loops. Can anyone see a mistake? I've attached the data file in case anyone wants to try to run it.
% code
load('trilobite_a.mat')
% plot original data
[N,M]=size(X);
figure;
hold on
col={'ro', 'r*', 'rd', 'bo', 'b*', 'bd', 'ko', 'k*', 'kd'};
for ii = 1:N;
hold on
for k=1:length(col);
plot(X(ii,:), col{k});
end
end
hold off
legend('A1','A2','A3','D1','D2','D3','P1','P2','P3');
xlabel('Variable');
ylabel('Trilobite Measurements');

  0 Comments

Sign in to comment.

1 Answer

Answer by dpb
on 2 Apr 2016
 Accepted Answer

for ii = 1:N;
for k=1:length(col);
plot(X(ii,:), col{k});
end
end
You're plotting the same data over again for each color combination since the inner loop iterates over the symbols for every row of X. So, the only thing visible in the end will be the last symbol; it'll overplot each time on the same location so it'll probably end up looking like a blob since hold in on the previous won't be erased.
I'm guessing you intend and there are the same number of symbol combinations as there are rows in X, so
col={'ro', 'r*', 'rd', 'bo', 'b*', 'bd', 'ko', 'k*', 'kd'};
figure;
for ii = 1:N;
plot(X(ii,:), col{ii})
if ii==1, hold on, end % only need to tell it once... :)
end
should work.
I'll note if you orient the data by column, then plot will interpret each column as a variable and you won't need to loop at all. See the doc's for plot and examples for the gory details.

  0 Comments

Sign in to comment.