Time on y axis - Matlab - matlab

I have plotted a figure with imagesc. The data was extracted from a file and the time was readed as a string column so I converted to double with datenum.
time = datenum(time, 'HH:MM:SS');
figure(1)
imagesc(freq,time,rssimat);
colorbar;
In this way it works but the picture looks like this
As you notice the y axis has this double numbers which represent the time converted with datenum. I actually would like to see my actual time like 'HH:MM:SS'.
Therefore I added a line:
datetick('y', 'HH:MM:SS');
And now my figure looks like...

It seems like you need to "zoom in" on Y axis. Your tracked interval appears to be very short in duration while on the Y axis you display the whole day.
Did you try: datetick('y', 'HH:MM:SS', 'keeplimits'); ? This will basically zoom in and fill the entire chart, while on the Y axis you will have HH:MM:SS format.
If you use datetick('y', 'HH:MM:SS', 'keepticks'); it will preserve the locations of the ticks while converting the labels to HH:MM:SS format.

u can use ylim function in matlab to limit your y-ranges

Related

Matlab date help on x-axis

I have a porkchop plot that looks similar to this:
The contour function used to make it has input arguments for the x and y positions that are serial dates (as that seemed to be required by MATLAB). Then I used the following command to get the format I want:
datetick('x', 2); datetick('y', 2);
The problem I am having is that when I zoom in on the plot the tick labels to not autogenerate and I can be left with no ticks on the x or y axis if I zoom in to use a weeks' date range for example.
I tried enabling 'auto' for XtickMode and YtickMode but when I zoom in or pan after using those commands the relationship between the independent and dependent variables are lost for some reason (aka the dates don't stay with data like they do when you just have numbers on the x axis and zoom in).
Any ideas on how to solve this issue to get the functionality I'm looking for?
I have also tried the command xtickformat('dd-MMM-yy') but I get an error "Invalid numeric tick label format." when I use it with the contour plot.
As far as I know there is no builtin method in MATLAB to do this. I use the datetickzoom function from the MATLAB FileExchange. If you replace all instances of datetick with datetickzoom, it will automatically update the relevant axis labels when you zoom in.

Display first date in MATLAB plot

I want to display the very first date in below figure which is the 1.1.1997
Thats my syntax
plot(MATLABDate(1:end-2),sumbeta)
datetick('x','mm/yyyy','keeplimits')
Haven't found how to do...
You can use the xlim function to set x-axis limits. If you only want to modify the lower limit, you can directly modify the value in the current x-axis:
>> hAx = gca;
>> hAx.XLim(1) = datenum('1/1/1997');

Removing scientific notation in plot axis (Matlab) [duplicate]

Tick labels for ticks bigger than about 10'000, get formatted to 1x10^4 for example. Whereas the exponential part appears above the corresponding axes. This misbehavior has been well described on on matlab central as well, but without a solution.
Thanks for your help.
The 'quick trick'
set(gca, 'YTickLabel',get(gca,'YTick'))
did not work when applied to bar3, as can be seen on the following figure.
EDIT
According to this technical solution page, the recommended way of formatting the tick labels is this (you can use any of the number formatting functions like NUM2STR, SPRINTF, MAT2STR, or any other..)
y = cool(7);
bar(y(:,1)*1e6)
set(gca, 'YTickMode','manual')
set(gca, 'YTickLabel',num2str(get(gca,'YTick')'))
However there seems to be a bug when it comes to the Z-axis (the labels are correctly formatted, but the exponential multiplier is still showing for some reason!)
y = cool(7);
bar3(y*1e6, 'detached')
set(gca, 'ZTickMode','manual')
set(gca, 'ZTickLabel',num2str(get(gca,'ZTick')'))
Finally, there's another workaround where we replace the tick labels with text objects (see this technical solution page as reference):
y = cool(7);
bar3(y*1e6, 'detached')
offset = 0.25; Xl=get(gca,'XLim'); Yl=get(gca,'YLim'); Zt=get(gca,'ZTick');
t = text(Xl(ones(size(Zt))),Yl(ones(size(Zt)))-offset,Zt, num2str(Zt')); %#'
set(t, 'HorizontalAlignment','right', 'VerticalAlignment','Middle')
set(gca, 'ZTickLabel','')
One other trick you can try is to scale your data before you plot it, then scale the tick labels to make it appear that it is plotted on a different scale. You can use the function LOG10 to help you automatically compute an appropriate scale factor based on your plotted values. Assuming you have your data in variables x and y, you can try this:
scale = 10^floor(log10(max(y))); %# Compute a scaling factor
plot(x,y./scale); %# Plot the scaled data
yTicks = get(gca,'YTick'); %# Get the current tick values
set(gca,'YTickLabel',num2str(scale.*yTicks(:),'%.2f')); %# Change the labels
One way to get better control over tick labels, and to avoid the exponential formatting, is to use TICK2TEXT from the File Exchange.
Here's an example:
y = cool(7); %# define some data
ah = axes; %# create new axes and remember handle
bar3(ah,y*1E6,'detached'); %# create a 3D bar plot
tick2text(ah, 'ztickoffset' ,-1.15,'zformat', '%5.0f', 'axis','z') %# fix the tick labels

How do I place the Date and Time on my horizontal axis to be in separate rows in a figure in MATLAB?

I am plotting a graph of random data versus datetime strings. These datetime strings are all in one line and serve as labels on the horizontal axis. I would like the date to be displayed in one line and the time to be in another line.
The image below describes my graph. I would like the time to be displayed underneath the date.
The code I used to generate the graph is shown below:
figure
plot(sample(:,1),sample(:,2:5),'o')
tick=get(gca,'xtick');
set(gca,'xticklabel',datestr(tick,31))
drawnow()
How can I get the time of each horizontal label to appear below the date?
This is a bit of a hack, but you can introduce the TeX command \newline inside the date string in between the spaces of the date and the time, then set these new strings to be the X Tick labels in your figure. You'll need to convert the character array that is output from datestr into a cell array of strings with cellstr, then use strrep to perform the replacement from a space to \newline.
Something like this should work:
figure
plot(sample(:,1),sample(:,2:5),'o')
tick=get(gca,'xtick');
vals = cellstr(datestr(tick,31)); %// Change
new_vals = strrep(vals, ' ', '\newline'); %// New
set(gca,'xticklabel',new_vals); %// Change
drawnow()
When I do this with a sample plot:
plot(1:6, 1:6, 'r.');
I get this as the final result once I run the code:
Take note that this assumes that the X Ticks are sufficiently spaced apart from each other. Your plot will look cluttered if you have many ticks on the horizontal axis.

How to switch Matlab plot tick labels to scientific form?

I have a semilogarithmic plot which works so far with semilogx. Now I would like to change the tick labels. Now it says 10^8 10^9 ... 10^13, but I would like to see 1e8, 1e9, ... 1e13 on the x-axis. How can I change that?
Cheers
Manuel
You can change the XTickLabels property using your own format:
set(gca,'XTickLabels',sprintfc('1e%i',0:numel(xt)-1))
where sprintfc is an undocumented function creating cell arrays filled with custom strings and xt is the XTick you have fetched from the current axis in order to know how many of them there are.
Example with dummy data:
clear
clc
close all
x = 0:100000;
y = log(x);
figure
semilogx(x,y)
xt = get(gca,'XTick');
set(gca,'XTickLabels',sprintfc('1e%i',0:numel(xt)-1))
Output: