How to plug in cell arrays in the x-axis of a plot? - matlab

I would like to make a plot in which I will the x-axis contains the values of a cell array.
I am using R2019a.
t.Format = 'dd-MMM-yyyy'
time = cellstr(t)
A = randn(3,1)
figure;
filename = plot(datetime(time),A);
xlim([min(datetime(time)) max(datetime(time))])
As an output, I am getting a graph that contains dates and time in the x-axis. The desired output should be limited in showing only the dates.

Use datetick:
datetick('x','dd-mmm-yyyy')

Related

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

How to display statistics as text in a graph?

I am using cdfplot in matlab to plot the empirical CDF of certain quantities.
[h,stats]=cdfplot(quantity)
Now stats returns me a structure having min, max, mean etc. I want these values to be displayed as text in the graph.
I have lot of similar graphs to plot and do not want to do it manually.
To put a text on a plot you use the text function. Here is a quick example on one plot:
y = evrnd(0,3,100,1);
[h, stats] = cdfplot(y);
hold on
x = -20:0.1:10;
f = evcdf(x,0,3);
plot(x,f,'m')
legend('Empirical','Theoretical','Location','NW')
stat_type = {'min: ';'max: ';'mean: ';'median: ';'std: '}; % make titles
stat_val = num2str(struct2array(stats).'); % convert stats to string
text(-15,0.7,stat_type) % <-- here
text(-11,0.7,stat_val) % <-- and here
hold off
This will give:
and you can use it inside a loop to do it for all the graphs.
The tricky thing is to define where to put the text on the graph. Here I choose (-15,0.7) and (-11,0.7) as a fixed points where I know there is no data. You should look at your plots, and find the correct place for that.

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

Date tick label on plotyy

I'´m trying to plot two variables on a same graph using plotyy. The problem is, when I configure x-axis for date labels, it appears both date and another kind of number labels.
How can I set only date labels?
Below it follows the script:
prp_dados = importdata ('c:\experimental\telemetricas_ANA\prp\29070100PARAUAPEBAS_1332013-2032013.txt');
cot_dados = importdata ('c:\experimental\telemetricas_ANA\cota\29070100PARAUAPEBAS_1332013-2032013.txt');
p_date_string = strcat (prp_dados.textdata(2:end,1),{' '}, prp_dados.textdata(2:end,2));
c_date_string = strcat (cot_dados.textdata(2:end,1),{' '}, cot_dados.textdata(2:end,2));
p_xdate = datenum (p_date_string, 'dd/mm/yyyy HH:MM');
plotyy (p_xdate,cot_dados.data,p_xdate,prp_dados.data);
set(gca,'XTick',p_xdate)
datetick('x', 'dd-mm-yyyy');
Rather than using gca to get only one handle, try using the syntax
[ax,h1, h2] = plotyy()
then you can set the properties for each plot individually.