필터 지우기
필터 지우기

Plot outside a for loop with different plotstyles

조회 수: 1 (최근 30일)
Christina
Christina 2015년 6월 9일
댓글: Ingrid 2015년 6월 9일
Hello all,
I've got a vector a with 20 values and a for loop, through which I want to plot the variables var1 and b
%%%%%%
%Define the variables
a = 1:20;
b = 0:0.01:1;
n= numel(a);
m=numel(b);
var1 = zeros(n,m);
***********************
%% For loop
for i = 1:n
var1(i,:)=(2+b).^(a(i));
end
****************************
At the end, var is a matrix sized: 20 x 101 and b is a vector 1 x 101.
When I am plotting
figure
plot(b,var1)
%%%%%%%%%%%%%%%%%%%%
I've got a figure with 20 lines, however, these are specified by default. I need to define the colours and the plot styles for every single of those lines
For example, I want the 1st line to be --> b-,
the 2nd--> r-
the 3rd--> m-
the ..... 15th --> b.
and so on...
Any thoughts on that ?
Many thanks!

답변 (1개)

Ingrid
Ingrid 2015년 6월 9일
doc plot
will learn you this:
h = plot(_) returns a column vector of lineseries handles, where h contains one handle per line plotted. When multiple lines are present, you can make changes to properties of a specific line by specifying its handle.
so use this handle in combination with set There is even a complete example worked out showing to do what you want (Change Line Properties Using Handles) but you can also automize this in a for loop when you store your colors and linestyles in a matrix
  댓글 수: 2
Christina
Christina 2015년 6월 9일
Hi Ingrid,
When I am plotting
figure
plotStyle = {'b-','r-','g-','m-','c-','k-','y-', 'b.','r.','g.','m.','c.','k.','y.','b--','r--','g--','m--','c--','k--'};
plot(b,var1,plotStyle{i})
either in the for loop or outside the for loop, I just get a graph, where all of the 20 lines have the properties of the last style, in that case k--
How can I resolve this?
Ingrid
Ingrid 2015년 6월 9일
try
plot(b,var1(:,i),plotStyle{i})
inside the for loop
you need to index your var1 variable because now you are just plotting all 20 lines with the same linestyles 20 times, hence only showing the last style as this one is on top.
If you would open the editor you would see this behaviour

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by