HELP with for loop plots and changing symbol styles

17 ビュー (過去 30 日間)
Lindsay Anderson
Lindsay Anderson 2016 年 4 月 2 日
回答済み: dpb 2016 年 4 月 2 日
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');

採用された回答

dpb
dpb 2016 年 4 月 2 日
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 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by