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

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

Related

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 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');

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))

How To Put String Labels on Contours for Contour Plots in MATLAB

I am wondering if it is possible to label the contours of a MATLAB contour plot with a set of user-defined strings?
I am currently using the following code snipper to produce a labelled contour plot:
%Create Data
X = 0.01:0.01:0.10
Y = 0.01:0.01:0.10
Z = repmat(X.^2,length(X),1) + repmat(Y.^2,length(Y),1)';
%Create Plot
hold on
[C,h] = contourf(X,Y,Z);
%Add + Format Labels to Plot
hcl = clabel(C,h,'FontSize',10,'Color','k','Rotation',0);
set(hcl,'BackgroundColor',[1 1 1],'EdgeColor',[0 0 0],'LineStyle','-',)
hold off
The issue with this code is that the labels are automatically generated by MATLAB. Even as I can easily change the contours that are labels, I cannot change the labels that they get.
Ideally, I would like to label them with a set of strings that I define myself. However if that is not possible, then I am wondering if it is possible to change the numeric format of the labels. The reason for this is that the code above actually produce a contour plot for an error rate, which I would like to display as a % value (i.e. use 1% in the contour label, instead of 0.01 etc.).
In this case, hcl is actually an array which stores handles to every contour label on your plot. When you set properties using the array (as in your code),
set(hcl, 'name', 'value')
You will set the property of every label to the same value.
You can change the properties of individual labels by iterating over the array. For example, this is how you would add a percentage sign:
for i = 1:length(hcl)
oldLabelText = get(hcl(i), 'String');
percentage = str2double(oldLabelText)*100;
newLabelText = [num2str(percentage) ' %'];
set(hcl(i), 'String', newLabelText);
end

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.