How to set X-axis units to datetime of empty plot in Matlab? - matlab

I have created figure with figure; and didn't plot anything on it.
How can I set it's x-axis to be datetime from this situation?
Entering
xlim([startTime, endTime]);
causes
Limits must be a 2-element vector of increasing numeric values.
How to accomplish?

Since the xlim function accepts a vector of two numeric values (the first one being the minimum and the second one representing the maximum), you can convert your datetime values into the required numeric values by using the datenum function. For example:
xlim([datenum(startTime) datenum(endTime)]);
In order to make your x-axis readable, you can make a call to datetick in order to convert the tick labels back to a valid datetime format. For example:
datetick('x','dd-mm-yyyy');
Here is a very basic example:
date_beg = datetime(2016,01,01);
date_end = datetime(2017,12,31);
date_seq = date_beg:date_end;
x = datenum(date_seq);
y = 1:numel(date_seq);
plot(x,y);
datetick('x','dd-mm-yyyy');
If you want to preserve the x-axis limits, call the datetick function passing the keeplimits argument. Here is a code snippet that reproduces your attempt:
date_beg = datetime(2016,01,01);
date_end = datetime(2017,09,15);
figure();
xlim([datenum(date_beg) datenum(date_end)]);
datetick('x','dd-mm-yyyy','keeplimits');

Related

Plot with logarithmic y axis having x axis type as datetime in Matlab

I have to construct a semi log plot with logarithmic y axis. My x axis type is datetime.
In general, to accomplish this, one can use semilogy command. Below written sample code gives a gist of what I would like to get done:
clear;clc;close
StartDate = datenum('12-Oct-2012 16:50:00');
EndDate = datenum('12-Oct-2012 17:50:00');
NoOfYAxisDataPoints=20;
NoOfTickMarks=5;
xAxisData = linspace(StartDate,EndDate,NoOfYAxisDataPoints);
yAxisData=linspace(1,25,NoOfYAxisDataPoints);
figure
semilogy(xAxisData,yAxisData)
xTickData = linspace(StartDate,EndDate,NoOfTickMarks);
ax = gca;
ax.XTick = xTickData;
datetick('x','HH:MM','keepticks')
Here both x and y axis data are double type.
But how do I accomplish my objective if the x axis data is not double, but datetime type as shown here:
Year = 2012;Month = 10;Day = 12;
Hours = 16;Minutes=(50:5:110)';Seconds= 00;
Time=datetime(Year,Month,Day,Hours, Minutes, Seconds);
figure
plot(Time,linspace(1,25,13))
xTickData = linspace(datenum(Time(1)),datenum(Time(end)),NoOfTickMarks);
ax = gca;
ax.XTick = xTickData;
datetick('x','HH:MM','keepticks')
When I change plot to semilogy, I am getting an error message “Error using semilogy. Non-numeric data is not supported in 'Line'”. But, the documentation of semilogy states that “The values in Xn can be numeric, datetime, duration, or categorical values. The values in Yn must be numeric.”. Even though my x axis values are datetime type, I am getting this error. What am I doing wrong? How do I accomplish this task?
I am using Matlab2016a
It sounds like your Matlab version does not support datetime as x-axis for semilogy. At least R2019b works fine for me. You can easily change y-scale after plot:
ax.YScale = 'log';

How to draw a rectangle between "datetime" data type and a numeric data type in MATLAB

assume I have X,Y
x=[03/01/2017,24/01/2017]% datetime type
y=[1000,2000] % numeric value
How can I draw a rectangle between those "coordinates" I am having trouble since one of them its DateTime value and the other one is numeric. using a plot function?
thanks.
The date time can't be treated as coordinates. If you are so sure to plot the rectangle you may need to convert the date time into a serial date number...
x = datenum(x);
try:
x = datetime([2017,2017],[1,1],[3,24]); % x = [03/01/2017,24/01/2017]
y = [1000,2000];
fill(x([1,2,2,1]),y([1,1,2,2]),'red')
It doesn't matter that x is datetime and y is numeric

How do I create an axis with dates in ILNumerics plots?

I am trying to create a 2D scatter plot with DateTime values along the x-axis using ILNumerics. From my research this can be achieved by:
Using integer DateTime ticks (DTTs) as the DateTime position coordinate
Writing a LabelTransformFunc to create the DateTime string (label expression) for the DTT value of a given axes tick
timeSeriesPlotCube.Axes.XAxis.Ticks.LabelTransformFunc = (ind, val) =>
{
var tickDateTime = new DateTime ((long)val)
return tickDateTime.ToString("dd-MMM-yy");
};
The problem I have is the automatic axes scaling factor that ILNumerics applies to large/small tick values is not provided in the val parameter passed to the LabelTransformFunc and therefore I cannot directly convert the val parameter to a DateTime string (the val parameter is the DTT value divided by the scaling factor).
My question is three-fold:
Can the automatic axes scaling factor be switched off?
How can I read the value of the axes scaling factor?
Are there any alternative approaches to implementing such a basic chart?
I have tried various approaches to (2) including reading the text value of the ScaleLabel, but timeSeriesPlotCube.Axes.XAxis.ScaleLabel.Text is always empty.
Thanks,
Len

Setting a specific number of tick marks on MATLAB plot

I am trying to figure out how to set a custom number of tick marks on the x-axis of a plot I am creating in MATLAB, but I am not sure if this is possible. I saw this question that seems to be asking the same thing, but that answer wasn't helpful since
set(gca,'XTick',tickVector);
sets the location of the ticks, not the number of ticks.
My code is as follows.
rangeBegin = 100000;
rangeEnd = 200000;
numberOfXTicks = 5;
plot(data(:, 1));
xAxisVals = linspace(rangeBegin, rangeEnd, numberOfXTicks);
%set(gca,'XTick',rangeBegin:rangeEnd); % Doesn't work as expected
set(gca,'XTickLabel',xAxisVals);
So in this example, I am just looking for a way to force MATLAB to create the plot with 5 ticks on the x-axis in order to match the 5 XTickLabels that I have set.
data is an array of doubles that is roughly <3000x1>.
EDIT: I should also add that I want my x-axis values to be from a separate array. The data array shown above corresponds to a time array (not shown...my bad), and each value in the data array has a corresponding value in the time array. Since I am selecting a range from the data array, I want to select the corresponding time values and use those as the x labels. But obviously I do not want 3000 time labels on my x-axis.
Hopefully this is more clear.
numberOfXTicks = 5;
h = plot(data(:, 1));
xData = get(h,'XData');
set(gca,'Xtick',linspace(xData(1),xData(end),numberOfXTicks))

scatter plot with its own ID

i am trying to plot data labels in a scatterplot.
For example i have vector
X=[0,1,2,3,4,2,1,0];
Y=[0,9,2,6,2,1,1,0];
z = 1:size(X,2);
scatter (X,Y)
i am using
for A = 1:size(X,2);
text(X(A),Y(A),z(A));
end;
however MATLAB state that Error using ==> text String argument expected after 2 or 3 numeric arguments
What should i do to add the ID in each pairs of x and y and also how to display the ID that has the same value of X and Y.
Thanks a lot in advance.
Change z(A) to num2str(z(A)), since text() expects it to be a string.