How to draw a graph with a colormap from a txt 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

 Accepted Answer

jonas
jonas on 9 Oct 2018
Edited: jonas on 10 Oct 2018
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))

17 Comments

Dear jonas, Thanks for your answer. I am fairly new in Matlab. Its the first time I am using these functions. I uploaded my excel file. The first 3 columns has to be assigned to X axis. The 7 different depth as Y axis and the data of soil moisture as Z axis. I know I have to learn more about the basis of making a matrix in matlab but in this case can you help me with this coding so it look like image2 with a colormap legend? I would appreciate your help so much.
jonas
jonas on 10 Oct 2018
Edited: jonas on 10 Oct 2018
There you go! I've updated the answer. It is not as smooth as the image. It is quite easy to fix by interpolation, which I can demonstrate if needed. Also, I filled the missing values, just remove that row if you want!
Thanks so much Jonas. You saved me! I deleted the first row with the names and also all the "Nan" rows and updated the xlsx file. Yes would you please show me how I can make it smooth so it looks like the image2? This is very good but I need to show the values better if its possible for you? In the "some aestethics" part of your code, I dont understand the question marks on the axis. It can have exactly the same names as the image. How can I show that? Also may I ask about the function of the last 3 lines of the code? Can we change the colors of the color map too? If I want to use imagesc function, Can I use the same code only change the last line? Do you think imagesc might give better looking results? Sorry to ask these! Thanks again
jonas
jonas on 10 Oct 2018
Edited: jonas on 10 Oct 2018
Updated again. This is the best I can do! Note that I still used the old excel file. I've commented a lot so you can understand the steps and adapt the code for your future plots! I inserted the (?) as I wasn't sure about the units :)
Thank you so much Jonas. It is very nice. If I use the new excel file which has no Nan data in it and no empty first rows or names, how should I change this line?
d=data(1,~isnan(data(1,:))); data(1,:)=[];
Jonas, I just run it and I got this error: "Undefined function 'datetime' for input arguments of type 'double'." What did I do wrong? I just copied the code and run it. What happened to the datetime??
What matlab version are you running? If it's very old then datetime will not be available. Or maybe the older versions of datetime do not support double input? I don't know. Anyway, I've attached a script using the old date format instead. This will also work with contourf plots.
When you import the data, the first row describes the headers year, month, day but also the soil depths. If you remove this in the excel file itself, then you can remove those two lines
d=data(1,~isnan(data(1,:)));
data(1,:)=[];
but you must then also add the depths manually:
d = [5 10 20 30 50 70 100]
Note also that, because you removed the NaNs, the function interpolates even long periods of missing data per default. This is kinda hard to change, and I suggest you keep those NaNs where you prefer to have gaps in your data.
Thanks. Its R2014a. (8.3.0.532). It does not work with datetime? I used the new matlab file you attached, it gave this errors:
Error using colorbarv6 (line 71) Unknown command option.
Error in colorbar (line 93) cbar = colorbarv6(varargin{:});
Also it doesnt give the name of the axis or colorbar. I attached the result. Is it because of my version or I am making a mistake? What version should I have to be able to run your first code with all of it?
jonas
jonas on 10 Oct 2018
Edited: jonas on 10 Oct 2018
I think it works with datetime but many functions have been updated since 2014a, so the syntax may have changed. If possible, you should update to 2018b. There are lots of new features that you will find useful if you use MATLAB a lot.
Anyway, if you attach your matlab script I can take a look. It says errir in line 71/93, but the script is only 36 lines, so either you did not copy the entire error message, or you've added something to the script.
After reading the old documentation, I think it may be that you cannot pass the axes handle to the colorbar in that version.. Try changing to this Just change this line
cb = colorbar('location','southoutside');
if it still does not work, change it to:
cb = colorbar;
Dear Jonas, I attached the matlab script and a screenshot of the error. Thanks. I will update to upper version but right now I have to make this work.
Yepyep, don't worry we are almost there. Did you try making the change I suggested in the previous comment? i.e. from this
cb = colorbar(gca,'location','southoutside');
REMOVE
to this
cb = colorbar('location','southoutside');
I am guessing that should solve this issue, but I am anticipating more issues in the code that follows ;)
So fortunately it gave the color bar but it still does not show all the code as it is such as the name of the bar and also the day and month times on X axis is wrong. I attached the files. Thanks Jonas :D
jonas
jonas on 10 Oct 2018
Edited: jonas on 10 Oct 2018
Right, the xticklabel rotation was not introduced in that release. When I get back to my computer I will link you to a FEX function that leta you do that.
Other than that it looks ok!
EDIT: download and put in your matlab path
https://se.mathworks.com/matlabcentral/fileexchange/45663-xyrotalabel-rotate-x-axis-and-y-axis-labels
I downloaded and put it but it did not change. The problem is mainly the numbers in the X axis that are shown wrong (The month specially is 00). I think its easier for me to try and get the 2018 version! Thanks Jonas for all your effort. Im so grateful.
Hehe yes, of course you must also adjust the code :)
I will post something in an hour or so, but as you said it is best to just download the new version instead of stitching together a buggy code
Cheers!
Attached is an updated script. The function I linked before was quite annoying, so I used another one. Here is the link . Download it, unzip and put the .m-file in the in the MATLAB search path, for example the same folder as your script, and then execute the attached script. I'm fairly sure it will work without additional issues.
You should take a look at the Getting started with MATLAB links. This forum is great, but you need to be able to do some basic coding yourself.
Thanks so much Jonas. I will take your advice and read it. Thanks again.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!