xlim function error. i have seen on video, the codes were working but with me it is not working

**My codes are below. I get that error: "Error using xlim (line 31) Limits must be a 2-element vector of increasing numeric values."
Also xtickformat function gives me eror. Can you help me? Thanks**
clc; clear; close all;
dates=datetime(2025,8,17)+calmonths(2:11)+calweeks(0:9)
xlim(datetime([2025 2026],[11 5],[12 23]))
xtickformat('dd-MMM-yyyy')

 採用された回答

dpb
dpb 2021 年 6 月 29 日
You're trying to set axis limits on a numeric axes with datetime values -- no can do...
dates=datetime(2025,8,17)+calmonths(2:11)+calweeks(0:9); % create your date vector
figure % make new figure so is clean
xlim(datetime([2025 2026],[11 5],[12 23])) % try your operation
xtickformat('dd-MMM-yyyy')
Error using xlim (line 31)
Limits must be a 2-element vector of increasing numeric values.
>>
And, as you saw, boom! Because the default axes created is numeric -- and xlim isn't strong enough to change them to a DatetimeRuler object on its own.
>> plot(dates,randn(size(dates))) % plot a datetime object first
>> xlim(datetime([2025 2026],[11 5],[12 23])) % now you can set the limits as wanted
>>
MORAL: Always make sure you've got the right type of axes object first -- with datetime, need to have put some data on the axes first with plot() or other higher-level function to create the proper type.

6 件のコメント

Thanks, that one below works quite fine. However I understood your point of view that the axes are just for numerical values.
dates=datetime(2025,8,17)+calmonths(2:11)+calweeks(0:9);
plot(dates,randn(size(dates)))
xlim(datetime([2025 2026],[11 5],[12 23]))
dpb
dpb 2021 年 6 月 29 日
編集済み: dpb 2021 年 6 月 30 日
" point of view that the axes are just for numerical values. "
No, that's not what I said (nor my point of view :) ).
There are multiple KINDS of axes objects now, OTOMH there are
  1. NumericRuler
  2. DatetimeRuler
  3. DurationRuler
besides the old plain old axes.
To put anything on the axis that isn't compatible with the default numeric axis object, the axes has to be of the proper type first -- as the above code demonstrated, when an axes object is created from a graphics call that doesn't have the variable type associated as in your use of xlim with a datetime range, then it fails because, as Steven L points out in his answer as well as mine, you can't convert numbers directly to datetime or duration values.
If, OTOH, you plot onto the axes first with a variable of the type wanted on the axis, then the overloaded plot function is smart enough to create the proper axis type and then xlim works.
It's not that axes are only for numeric values, it's that there has to be an axis of the right type for the data to be displayed on it.
Originally, there was only one axes type (I don't remember just which release) but the other two came along with the introduction of datetime and friends. Prior to that, dates and time were represented by the venerable datenum which is just a double scaled to hold dates. Date axis labels were done with the klunky datetick function -- it worked but was pretty tortuous; datetime and integrated axes are far better but there is a price for progress -- and that price is that the axes have to match the data class as you've just discovered.
Here's a short exercise at the command line to illustrate...
>> whos t* % Illustrate types for a couple of time variables on hand...
Name Size Bytes Class Attributes
t 40002x1 320016 double
t2 1347x1 10776 datetime
>>
>> figure; hAx=axes % create a new axes, save its handle
hAx =
Axes with properties:
XLim: [0 1]
YLim: [0 1]
...
>> hAx.XAxis % look at the underlying XAxis object -- it's the
ans =
NumericRuler with properties:
Limits: [0 1]
Scale: 'linear'
Exponent: 0
TickValues: [1×11 double]
TickLabelFormat: '%g'
Show all properties
>> plot(t2(1:10),rand(10,1)) % plot a datetime variable onto the figure
>> hAx.XAxis % now it's a DatetimeRuler instead...
ans =
DatetimeRuler with properties:
Limits: [17-Jun-2021 12:46:30 17-Jun-2021 12:51:30]
TickValues: [Jun 17, 2021, 12:47 Jun 17, 2021, 12:48 Jun 17, 2021, 12:49 Jun 17, 2021, 12:50 Jun 17, 2021, 12:51]
TickLabelFormat: 'HH:mm'
Show all properties
>> plot(diff(t2(1:11)),rand(10,1)) % diff() datetime--> duration -- and now the axis has morphed yet again
>> hAx.XAxis
ans =
DurationRuler with properties:
Limits: [-25:11:30 25:12:30]
TickValues: [-24:00:00 -12:00:00 00:00:00 12:00:00 24:00:00]
TickLabelFormat: 'hh:mm:ss'
Show all properties
>>
"To every thing there is a season, ..."
I liked your detailed description. I practiced your codes several time and now it is all good!
dpb
dpb 2021 年 6 月 30 日
Great! Glad to help--it's a little subtle because the XAXis and YAxis objects are underneath the top level axes handle and the plot function is class-aware and so does the right thing but it is superficially transparent to the user.
Thanks a lot!
Check my codes please, they are working pretty fine.
history=datetime(2019,08,1)+caldays(0:10);
y=randi([11,21],1,11);
stem(history,y,'filled');
tarihBaslangic=datetime(2019,08,1);
tarihBitis=datetime(2019,08,8);
xlim([tarihBaslangic tarihBitis])

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

その他の回答 (1 件)

I would expect to see that error if you'd plotted with non-datetime data as your X data. If for example you'd plotted these points:
x = 1:10;
y = x.^2;
plot(x, y)
How much of that plot would you expect to see if I set the X limits from last Thursday to next Saturday?
T = datetime('today');
lastThursday = dateshift(T, 'dayofweek', 'Thursday', 'previous')
lastThursday = datetime
24-Jun-2021
nextSaturday = dateshift(T, 'dayofweek', 'Saturday', 'next')
nextSaturday = datetime
03-Jul-2021
plot(x, y)
xlim([lastThursday, nextSaturday]);
Error using xlim (line 31)
Limits must be a 2-element vector of increasing numeric values.
You haven't specified what error message you received when you tried calling xtickformat but I'm guessing the root cause is the same: you can't change numbers on the X axis into dates and times.

カテゴリ

ヘルプ センター および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by