Hi all,
I am busy solving several vectors inside a for loop. As each vector is solved I am trying to plot it before the values are overwritten in the next loop iteration. Additionally, I am comparing two methodologies for solving the vector, and therefore need to superimpose to plots on the figure with each loop. Due to the fact that both methods are so close this requires me to plot one line thicker than the other, and additionally one line must be dashed.
In the past i used the following method to plot inside a for loop:
colour = ['b','r','c','y'];
for i = 1:2
%arbitrary code that solves x,y,x2 and y2
plot(x,y,colour(i),x2,y2,colour(i+2))
end
How would i add additional plot editing such as linewidth and dashed lines to the above code?
Thank you in advance.

 採用された回答

Adam
Adam 2015 年 4 月 29 日

0 投票

If you do them as two separate statements you can give each the instructions you wish e.g
hold( hAxes, 'on' )
for i = 1:2
plot( hAxes, x, y, 'Color', colour(i), 'LineWidth', 2, 'LineStyle', '-' );
plot( hAxes, x2, y2, 'Color', colour(i+2), 'LineWidth', 1, 'LineStyle', '--' );
end
or whatever properties you wish to set.

2 件のコメント

Gareth Howard
Gareth Howard 2015 年 4 月 30 日
Thanks so much! Worked perfectly!
For interests sake; what is the reason behind the hold(hAxes,'on') line of code. I got an error of undefined hAxes variable when running this. Instead I just used a simple hold on after each plot.
Thanks once again!
Adam
Adam 2015 年 4 月 30 日
hAxes would be the handle to your axes. I never (well, not deliberately at least) work with implicit axes handles nowadays when plotting as I work with a lot of executables containing multiple windows and too often I have ended up with a plot appearing in a figure it wasn't supposed to after which I quickly learned to always use explicit handles for figures and axes.
In a general case just using
hold on
and a plot instruction without an explicit axes works fine. I just don't like it because it isn't exact and can lead to problems in more complex code.
So, for example, if I am creating a new figure I usually do something like:
figure; hAxes = gca;
or even
hFig = figure; hAxes = gca;
if I want the figure handle too (usually I am only interested in the axes). Putting the instructions on the same line guarantees that your hAxes will be the axes you expect (unless it is conceivably possible for a human to click a different figure between the machine fulfilling those two instructions!

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

その他の回答 (0 件)

カテゴリ

質問済み:

2015 年 4 月 29 日

コメント済み:

2015 年 4 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by