MATLAB: How to draw a graph with a colormap from a txt file

colormapgraphtext file

I have a txt file consisting of 10 columns. The first 3 columns are year/month/day and the next 7 columns are my moisture data in 7 different depths. (this data set is for 3 years) 2014 1 1 10.2 13.4 14 15.1 17 . . . 2017 12 30 12.4 14 15.6 17 19.4
I wrote a code to plot a graph using datenume and plot function and it look likes image1. It is correct but now I am asked to make the same graph in the format of image2. I dont know what function draws a graph like image2 from my data set. Can anyone help me? Thanks

Best Answer

You can do that with e.g. surf or contourf . I would suggest surf because it works with datetime format, which you should use instead of datenum.
Here's a code adapted to your data
% Load data
data=xlsread('Soil misture.xlsx')
% Save depths, d, and remove this row
d=data(1,~isnan(data(1,:)));
data(1,:)=[];
% Save times, t, as datetime format
t=datetime(data(:,1),data(:,2),data(:,3));
% Save moisture content, u, as 2d matrix
u = data(:,4:end);
% Interpolate over time
ti = t(1):days(1):t(end);
ui = interp1(t,u,ti);
% Interpolate over depth
di = min(d):1:max(d);
uii = interp1(d,ui',di);
% plot
surf(ti,di,uii,'edgecolor','interp');
% some aestethics
ax=gca;
axis tight
view([0 90]);
cb = colorbar(gca,'location','southoutside');
cb.Label.String = 'Soil water content (cm^3/cm^3)';
ylabel('Soil depth (cm)');
xlabel('Time','fontweight','bold');
xtickformat('yyyy/MM/dd');
ax.XRuler.TickLabelRotation = 40;
ax.XRuler.TickDirection = 'out';
ax.YRuler.TickDirection = 'out';
ax.Layer = 'top';
ax.Color = 'none';
grid off
colormap(jet(20))