xlim not working in 'loop' plots..

4 ビュー (過去 30 日間)
Cem Menlikli
Cem Menlikli 2022 年 7 月 28 日
コメント済み: Cem Menlikli 2022 年 7 月 28 日
a=[1 2 3]; %input
for i =1 :1:3
b=a.*3;
c=a.*10;
end
subplot(1,4,1)
for i=1:3
plot(a,b);
end
xlim=([1 30])
subplot(1,4,2:4)
for i=1:3
plot(a,c);
end
xlim=([1 100])
  2 件のコメント
Jan
Jan 2022 年 7 月 28 日
Whenever you mention in the forum, that there is a problem, take the time to describe, what the problem is. "Is not working" is too lean. Explain the difference between the wanted and the obtained result or post a copy of the complete error message. This will increase your chances to get a useful answer fast.
Cem Menlikli
Cem Menlikli 2022 年 7 月 28 日
I really appreciate your tips. this is very helpful.. thought of simplifiying the code to explain the problem will help.. origonal code is a matrix eigenvalue problem and hence the multiplication.. was thinking it is something to do with indexed subplot function.. a good example when you are tird, you should stop and re-look at it next morning..

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

回答 (1 件)

Jan
Jan 2022 年 7 月 28 日
編集済み: Jan 2022 年 7 月 28 日
You do not call the function xlim() at all, but create a variable with the same name. Replace:
xlim=([1 100])
by
xlim([1 100])
without the = operator, which assigns a value to a variable.
The code contains several other problems:
a=[1 2 3]; %input
for i =1 :1:3
b=a.*3;
c=a.*10;
end
This executes the same assignments 3 times. The body of the loop does not depend on the loop counter, so you can omit the loop:
a = [1 2 3]; %input
b = a * 3;
c = a * 10;
Using the elementwise multiplication .* is not a problem here, but for multiplications with scalar the * operator is fine also.
subplot(1, 4, 1)
for i = 1:3
plot(a, b);
end
xlim([1 30]) % Without =, see above
This plots the same line 3 times. If there was no hold('on') command before, plot() removes all formerly drawn objext from the axes. So this is just a waste of time.
subplot(1, 4, 2:4)
for i = 1:3
plot(a, c);
end
xlim([1 100])
Same problem as above: The loop is useless.
Read the documentation of the for command again: for .
Note that there is a method, to define the XLimits using an assignment instead of a function:
AxesH = axes; % Or: AxesH = subplot(1, 4, 1), which replies the axes' handle also
AxesH.XLim = [1, 100];
Now XLim is not a variable, but a property of the Axes object.
Summary:
a = [1, 2, 3];
b = a * 3;
c = a * 10;
subplot(1, 4, 1);
plot(a, b);
xlim([1, 30]);
subplot(1, 4, 2:4);
plot(a, c);
xlim([1, 100]);
The readability of code is increased, if you insert spaces around operators. Writing vectors without separators saves some milliseconds during typing, but can cost hours for debugging. See:
[1 0]
[1+ 0]
[1 + 0]
[1 +0]
With commas as separators, the result does not not depend on blanks anymore and this is a good programming practice.

カテゴリ

Help Center および File ExchangeSubplots についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by