Make informative x-axis labels for long term spectrogram plot

19 views (last 30 days)
I have a spectrogram which spans one month worth of 2 minute recordings. Instead of labelling each two minute recording on the x-axis, I'd like to group the x-axis tick labels by day, so that the data is more readable and informative.
So far, I have converted each serial datenumber in my data to a date, but I am having trouble applying this to the data. I then tried to do this manually but that hasn't worked either.
Any tips much appreciated!
%% Time-domain plot (spectrogram)
figure(1); clf; set(figure(1),'color','w'); hold off;
%produce figure with axes
%x corresponds to columns of z (time)
%y corresponds to rows (frequency)
%A is transposed in third input to meet requirements of surf
%Plot uses x and y as coordinates-at x(time) and y(freq), what is A?
axis tight
set(gca,'tickdir','out','layer','top','fontname',... %change size of axes
%set(gca,'YScale','log'); %set y-axis scale to logarithmic
ylabel('Frequency [ Hz ]','fontsize',14);
c = colorbar; %insert colour bar
colorTitleHandle = get(c,'Title');
titleString = 'PSD [ dB re 1 uPa^2 Hz^-^1 ]';
set(colorTitleHandle ,'String', titleString,'Rotation',270,...
'Position',[60 300 2]); %positions colorbar axis
set(colorTitleHandle, 'fontsize', 14); %change colourbar font size
caxis([40 100]); % sets scale on colour bar
days=datetime(t,'ConvertFrom','datenum'); %group datenum values into day of month
xticks(days); %I get an error here which says 'value must be a vector of type single or double whose values increase'
xlabel('Date (MM-YY)')
Eric Sofen
Eric Sofen on 2 Oct 2020
It looks like you found a solution, but FYI, most MATLAB plot types support plotting datetime and duration directly, so instead of overwriting the xticks and the labels, you could do (referring to the variable names in your code above)

Sign in to comment.

Answers (0)


Find more on 2-D and 3-D Plots in Help Center and File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!