Can you change individual elements in a for loop each time?

조회 수: 2 (최근 30일)
Moose
Moose 2023년 4월 23일
댓글: Moose 2023년 4월 24일
Is it possible to change something non-numerical inside a for loop each time it goes through? I'm trying to make a different colored line on a graph every time it loops. If that's not possible, I'd love to know what else I could do to achieve the result.
To have some code to tweak I suppose it could be something like the following, but I'm trying to have each value of 'x' as represented on the graph a different color (first loop (1,5) a black circle, second (2,5) a red one, third (3,5) a blue, etc).
clc;clear;close all
figure
hold on
for x=1:10
y=5;
plot(x,y,'ko')
end

채택된 답변

VBBV
VBBV 2023년 4월 23일
편집: VBBV 2023년 4월 23일
clc;clear;close all
figure
hold on
col = {'ro','bd','k+'}
col = 1×3 cell array
{'ro'} {'bd'} {'k+'}
col1 = {'r-','b-','k-'}
col1 = 1×3 cell array
{'r-'} {'b-'} {'k-'}
% plots points
for x=1:3
y=5;
plot(x,y,col{x});
end
figure
hold on
% plots lines
n = [0.5 1 1.5];
for x=1:3
X=1:10;
plot(X,X.^(n(x)),col1{x});
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by