Changing line colors in nested for loop not working
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi there,
I am struggling to change line and marker colors in this nested for loop:
col = {'r', 'b'};
sym = {A, B};
x = [20 30 40 50];
matrix_A = [1 2 3 4; 5 6 7 8];
matrix_B = [9 10 11 12; 13 14 15 16];
figure
fig = tiledlayout(1,2)
for i = 1:length(sym)
t1 = nexttile(fig);
for j = 1:length(col)
plot(t1, x, eval(['matrix_' sym{i}]), 'color', col{j}); % plotting matrix_A and matrix_B
end
end
This nested loop plots 2 blue lines instead of a red and a blue
Thank you all
댓글 수: 0
답변 (1개)
Rik
2023년 4월 26일
편집: Rik
2023년 4월 26일
Why are you using eval? There is no real reason you should need it. Same goes for length: what you actually mean is numel (or perhaps size).
Anyway, the cause for this problem is that you are plotting your data twice, first with red and then with blue. You are not changing the color for each row of your data.
matrix_A = [1 2 3 4; 5 6 7 8];
matrix_B = [9 10 11 12; 13 14 15 16];
col = {'r', 'b'};
data = {matrix_A, matrix_B};
x = [20 30 40 50];
figure
fig = tiledlayout(1,2);
for i = 1:numel(data)
t1 = nexttile(fig);
for data_row = 1:size(data{i},1)
plot(t1, x, data{i}(data_row,:), 'color', col{data_row});
hold(t1,'on')
end
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!