How can I update the x axis limit inside a loop?

8 ビュー (過去 30 日間)
Muhamed Sewidan
Muhamed Sewidan 2020 年 1 月 27 日
コメント済み: Muhamed Sewidan 2020 年 1 月 27 日
% parameters
omav = 5*1000*2*pi; % omega average, center
sigma = [0,0.5,1,2,5].*1000*2*pi; % std deviation. A.K.A width
t = 100;
for i=1:length(sigma)
om = linspace(-3*sigma(i), 3*sigma(i),100);
num = -(om-omav).^2;
den = 2.*sigma(i).^2;
gaus = exp(num./den);
figure(i), plot(om, gaus)
title( [' for sigma = ' num2str(sigma(i)/(2000*pi))] )
set(gca,'xlim',[om(1) om(end)])
end
This is my code, i'm trying to make each figure with new x limits from the value of the variable omm but i get an error says:
" Error using matlab.graphics.axis.Axes/set
While setting the 'XLim' property of 'Axes':
Value must be a 1x2 vector of numeric type in which the second element
is larger than the first and may be Inf "
I thought this because of the zero value of first sigma, therefore i added an if statement that if i>1 , set the x limits, and the error disappeared, but the limits still as they were.

採用された回答

Adam Danz
Adam Danz 2020 年 1 月 27 日
On the first iteration, om is all 0s so [om(1) om(end)] returns [0,0] which, as you pointed out, is not allowed when setting axis limits. Here are two ways around that.
Method 1: offset one of the limit values by a very tiny number
set(gca,'xlim',[om(1), om(end)+realmin])
% --or--
set(gca,'xlim',[om(1)-realmin, om(end)])
Method 2: set a default limit when the limits are equal
xl = [om(1), om(end)];
if isequal(xl(1),xl(end))
xl = [0,1];
end
set(gca,'xlim',xl)
  2 件のコメント
Adam Danz
Adam Danz 2020 年 1 月 27 日
The xlim is working. Look at your data more closely and you'll see that the blue line terminates at the end of the x axis.
What were you expecting to see?
Muhamed Sewidan
Muhamed Sewidan 2020 年 1 月 27 日
I wanted it to show the whole plot, as you can see it is cutted. but, I'veuntitled.png found that this is the last value

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by