How to draw a rectangle between "datetime" data type and a numeric data type in MATLAB - 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

Related

How to add xlabel ticks from different matrix in Matlab?

I have a plot of UTCTime double values from a vector and Altitudes from another vector. I used the function:
>> plot(datenum(cellstr(UTCTime),string('yyyymmddHHMMSS')),Altitudes );
And it works fine. Well, now I have another two vectors on Longitudes and Latitudes ( same size of UTCTime vector ).
How can I put on ticks the corrispondent value of Latitudes and Longitudes at specific UTCTime ?
UTCTime : 2242x1 double
Latitudes: 2242x1 double
Longitudes: 2242x1 double
Using the xticks and xticklabels commands, you can specify the values and labels for the x-axis.
For example, if you wanted to have tick marks every 10 degrees Lat, you could say this:
xticks_mat = max(latitude):-10:min(latitude);
xticks(xticks_mat);
You then could use xticklabels to specify what your x-axis labels would say at each tick mark.

How to set X-axis units to datetime of empty plot in 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');

matlab problems calculating with image [duplicate]

I have a 1x50000 size matrix v and I want to convert it to zero mean and unit variance:
x = ((v-mean(v))/std2(v));
But instead of giving me exact floating point values MATLAB is converting it to nearest integers. Please help me in getting the exact values.
Check the data type for v. I'm sure it's an integer type, using integer arithmetic, which is why the result is an integer. You need to convert it to a floating point type to perform floating point operations on it:
v = double(v); % Convert v to a double-precision float
x = ((v-mean(v))/std2(v)); % Result is now a double as well

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

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.