Search code examples
matlabplottimematlab-figure

Changing seconds to UTC in Matlab


I am plotting data from a netcdf file in Matlab and the x-axis is supposed to be in UTC seconds. After plotting, I can't seem to figure out how to change it to the HH:MM:SS format.this is an example as you can see on the x-axis of what is happening I tried different things like:

time=datenum(x,'HH:MM:SS')

but they don't work. Here is a better examples of what I am trying to do: So the data for this flight was taken on Sep 10, 2010 and the data was recorded at "seconds starting at 09:00:00" and here is my code:

%time
x = double(netcdf.getVar(ncid,netcdf.inqVarID(ncid,'Time')));
%altitude
y_alt = double(netcdf.getVar(ncid,netcdf.inqVarID(ncid,'GGALT')));
%temperature
y_t = double(netcdf.getVar(ncid,netcdf.inqVarID(ncid,'ATX')));
%dew point
y_dp = double(netcdf.getVar(ncid,netcdf.inqVarID(ncid,'DPXC')));
%vertical wind speed
y_vws = double(netcdf.getVar(ncid,netcdf.inqVarID(ncid,'WIC')));
%horizontal wind speed
y_hws = double(netcdf.getVar(ncid,netcdf.inqVarID(ncid,'WSC')));
%liquid water content
y_lwc = double(netcdf.getVar(ncid,netcdf.inqVarID(ncid,'PLWCC')));
%ice water content
y_iwc = double(netcdf.getVar(ncid,netcdf.inqVarID(ncid,'PLWC1DC_LMI')));

figure
plot(x,y_alt, 'r')
ylim([0 15000])
title({'Altitude vs Time RF14'}, 'fontweight', 'bold', 'fontsize', 12);
xlabel('UTC (seconds)'); 
ylabel('Altitude (meters)');

figure
plot(x,y_t,'b',x,y_dp,'r')
ylim([-100 50])
title({'Temperature and Dew Point RF14'}, 'fontweight', 'bold', 'fontsize', 12);
xlabel('UTC (seconds)'); 
ylabel('Temperature (C)');
legend('Temperature', 'Dew Point');

figure
plot(x,y_vws,'b',x,y_hws, 'm')
ylim([-5 11])
xlim([0 20500])
title({'Wind Speed RF14'}, 'fontweight', 'bold', 'fontsize', 12);
xlabel('UTC (seconds)'); 
ylabel('Wind Speed (m/s)');
legend('Vertical Wind Speed', 'Horizontal Wind Speed');

figure
plot(x,y_lwc, 'm',x,y_iwc,'b')
ylim([-1 11])
title({'Liquid/Ice Water Content RF14'}, 'fontweight', 'bold', 'fontsize', 12);
xlabel('UTC (seconds)'); 
ylabel('H2O Content (g/m^3)');
legend('Liquid Water Content', 'Ice Water Content');

so the seconds that are present range from 0-19920. So I want to start from 10-Sep-2010 09:00:00 and end at 10-Sep-2010 15:32:00. For some reason I can't get the datum thing to work for me.


Solution

  • If your x-axis variable is in seconds, first divide by 86400 to transform it into units of one day, and then apply datetick with the desired date format.

    Here's an example

    x = 0:10:34e3; % x axis in seconds
    y = cumsum(randn(size(x))); % example y values
    plot(x/86400, y) % plot with x in days, not in seconds
    datetick('x', 'HH:MM:SS') % set date ticks in x axis
    grid on % optionally add grid
    

    enter image description here