Changing line colors in nested for loop not working
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Rik
am 26 Apr. 2023
Bearbeitet: Rik
am 26 Apr. 2023
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 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!