how to make automatic graph script with a for loop?

Hi everybody, i'm trying to make the folowing script for grahp 7 points, but i do not understand why only represent only one point of the seven that i should draw.
hold on
grid on
n=0;
k=1;
step=1;
for i=7
y=con1+step;
x=1+step;
plot(x, y, '*');
k=k+1;
end
This are the 7 points
con1 =4.3e10
con2 =1.16e10
con3 =5.44e9
con4 =3.15e9
con5 =1.98e9
con6 =1.4e9
con7 =1.05e9
Can anyone help me to structure it better the code?. Thanks in advance

 採用された回答

KSSV
KSSV 2016 年 10 月 18 日

0 投票

con = [4.3e10 ; 1.16e10;5.44e9;3.15e9;1.98e9;1.4e9;1.05e9 ] ;
hold on
grid on
n=0;
k=1;
step=1;
for i=1:7
y=con(i)+step;
x=1+step;
plot(x, y, '*');
k=k+1;
end

1 件のコメント

Tony Castillo
Tony Castillo 2016 年 10 月 18 日
Thanks for your help, in the code just lack x="(i)"+step; instead that x=1+step.
hold on grid on n=0; k=1; step=1; for i=1:7 y=con(i)+step; x=1+step; plot(x, y, '*'); k=k+1; end

サインインしてコメントする。

その他の回答 (2 件)

KSSV
KSSV 2016 年 10 月 17 日
編集済み: KSSV 2016 年 10 月 17 日

0 投票

Use hold on after plot command inside the loop...using hold on points are plotted on the same figure. If not every time new figure opens.

3 件のコメント

Tony Castillo
Tony Castillo 2016 年 10 月 17 日
編集済み: Adam 2016 年 10 月 17 日
Using the hold on function as you recommend me, i have the same results than before.
n=0;
k=1;
step=1;
for i=7
y=con1+step;
x=1+step;
plot(x, y, '*');
k=k+1;
hold on
grid on
end
Guillaume
Guillaume 2016 年 10 月 17 日
編集済み: Guillaume 2016 年 10 月 17 日
The hold command needs to be issued before plotting.
Of course, you also have the issue highlighted by Adam
Adam
Adam 2016 年 10 月 17 日
編集済み: Adam 2016 年 10 月 17 日
As long as the hold is before the 2nd plot it should work fine I would think. Of course ideally you would give the axes handle to the hold command
hold( hAxes, 'on' )
to avoid any unpleasant bugs creeping in, but in a simple script just
hold on
tends to work fine.

サインインしてコメントする。

Adam
Adam 2016 年 10 月 17 日
編集済み: Adam 2016 年 10 月 17 日

0 投票

Your for loop doesn't change anything so you just plot the same point every time. Neither con1 nor step changes so neither do x or y.
Did you mean to update step using 'i' or something like that? Or do you want con1 upto con7? In which case store them in a single array instead and access as
con(i)
rather than
con1

2 件のコメント

Tony Castillo
Tony Castillo 2016 年 10 月 17 日
When i do it, appear this error. Undefined function or variable 'con'.
Error in UntitledFOR (line 6) y=con(i)+step;
This is the code with your recommendation.
Adam
Adam 2016 年 10 月 17 日
Well, of course you have to define con as an array containing all your values instead of the 7 variables you currently have as con1 to con7.

サインインしてコメントする。

製品

質問済み:

2016 年 10 月 17 日

コメント済み:

2016 年 10 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by