How to plot date on x-axis when it is retrieved as 'text' from table.

2 views (last 30 days)
I want to plot time series from a .txt file. After importing the file as attached sample.txt. I run the following code to read the 5th and the 7th column. I am facing problem in retrieving the 'date' given in 5th column so that I can plot a scatter plot. The x-label should like 2013 2014 2015 ....
I would be grateful if you can help to resolve my issue.
data=sample;
[a,b]=size(data);
j=1;
n=500;
d=zeros(n,1); %Problem
for i=1:a
if strcmp("AREQ",data.SITE(i,1))
x(j)=data{i,7};
d(j)=datetime(data{i,5},'InputFormat','yyyy-MM-dd'); %Problem
j=j+1;
end
end
%% Plotting
A=x(1:j-1,:);
D=datetime(d(1:j-1,:));
figure;
scatter(D,A); % Problem...The x-axis should be in year 2013 2014 2015 ..
Thank you

Accepted Answer

Star Strider
Star Strider on 1 Sep 2022
The readtable function imports it as a datetime array —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1113585/sample.txt')
T1 = 67×21 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var15 Var16 Var17 Var18 Var19 Var20 Var21 ________ _____ ____ ____ __________ _____ ___________ __________ ______ _____ ________ ________ ________ _____ ___________ ___________ ___________ _____ ________ ________ ________ {'AREQ'} 56301 2013 9 2013-01-09 {'|'} 2.6245e+06 4.6466e+07 792.29 {'|'} 0.006992 0.002125 0.005306 {'|'} -2.7134e+05 5.8364e+06 2.5514e+06 {'|'} 0.002116 0.004828 0.007333 {'AREQ'} 56302 2013 10 2013-01-10 {'|'} 2.6245e+06 4.6466e+07 792.28 {'|'} 0.007329 0.002045 0.005595 {'|'} -2.7134e+05 5.8364e+06 2.5514e+06 {'|'} 0.002034 0.005107 0.00768 {'AREQ'} 56303 2013 11 2013-01-11 {'|'} 2.6245e+06 4.6466e+07 792.28 {'|'} 0.007111 0.001963 0.005342 {'|'} -2.7134e+05 5.8364e+06 2.5514e+06 {'|'} 0.001953 0.004836 0.007467 {'AREQ'} 56304 2013 12 2013-01-12 {'|'} 2.6245e+06 4.6466e+07 792.28 {'|'} 0.007199 0.00199 0.005435 {'|'} -2.7134e+05 5.8364e+06 2.5514e+06 {'|'} 0.001978 0.004932 0.007555 {'AREQ'} 57531 2013 144 2013-05-23 {'|'} 2.6245e+06 4.6466e+07 792.29 {'|'} 0.007362 0.001667 0.005361 {'|'} -2.7134e+05 5.8364e+06 2.5514e+06 {'|'} 0.001654 0.004773 0.007759 {'AREQ'} 56293 2013 1 2013-01-01 {'|'} -1.8221e+06 1.9234e+07 2488.9 {'|'} 0.007153 0.002007 0.004971 {'|'} 1.9428e+06 -5.8041e+06 -1.7969e+06 {'|'} 0.001317 0.004951 0.007326 {'AREQ'} 56294 2013 2 2013-01-02 {'|'} -1.8221e+06 1.9234e+07 2488.9 {'|'} 0.007185 0.002028 0.005038 {'|'} 1.9428e+06 -5.8041e+06 -1.7969e+06 {'|'} 0.001319 0.005028 0.007355 {'AREQ'} 56295 2013 3 2013-01-03 {'|'} -1.8221e+06 1.9234e+07 2488.9 {'|'} 0.007039 0.001971 0.004903 {'|'} 1.9428e+06 -5.8041e+06 -1.7969e+06 {'|'} 0.001283 0.004886 0.007208 {'AREQ'} 56296 2013 4 2013-01-04 {'|'} -1.8221e+06 1.9234e+07 2488.9 {'|'} 0.007311 0.001979 0.004904 {'|'} 1.9428e+06 -5.8041e+06 -1.7969e+06 {'|'} 0.001314 0.004844 0.007498 {'AREQ'} 56297 2013 5 2013-01-05 {'|'} -1.8221e+06 1.9234e+07 2488.9 {'|'} 0.007211 0.002011 0.00493 {'|'} 1.9428e+06 -5.8041e+06 -1.7969e+06 {'|'} 0.001347 0.004891 0.00739 {'AREQ'} 56298 2013 6 2013-01-06 {'|'} -1.8221e+06 1.9234e+07 2488.9 {'|'} 0.007641 0.002071 0.005183 {'|'} 1.9428e+06 -5.8041e+06 -1.7969e+06 {'|'} 0.001347 0.005134 0.007833 {'AREQ'} 56299 2013 7 2013-01-07 {'|'} -1.8221e+06 1.9234e+07 2488.9 {'|'} 0.007715 0.002148 0.005241 {'|'} 1.9428e+06 -5.8041e+06 -1.7969e+06 {'|'} 0.001452 0.005191 0.007908 {'AREQ'} 56300 2013 8 2013-01-08 {'|'} -1.8221e+06 1.9234e+07 2488.9 {'|'} 0.008008 0.002159 0.005381 {'|'} 1.9428e+06 -5.8041e+06 -1.7969e+06 {'|'} 0.001419 0.005319 0.008212 {'AREQ'} 56301 2013 9 2013-01-09 {'|'} -1.8221e+06 1.9234e+07 2488.9 {'|'} 0.00731 0.002007 0.00491 {'|'} 1.9428e+06 -5.8041e+06 -1.7969e+06 {'|'} 0.001357 0.004851 0.007496 {'AREQ'} 56302 2013 10 2013-01-10 {'|'} -1.8221e+06 1.9234e+07 2488.9 {'|'} 0.007659 0.002114 0.005191 {'|'} 1.9428e+06 -5.8041e+06 -1.7969e+06 {'|'} 0.001417 0.005139 0.007851 {'AREQ'} 56303 2014 11 2014-01-11 {'|'} -1.8221e+06 1.9234e+07 2488.9 {'|'} 0.007443 0.002034 0.00495 {'|'} 1.9428e+06 -5.8041e+06 -1.7969e+06 {'|'} 0.001391 0.004877 0.007636
T1.Var5 % Check
ans = 67×1 datetime array
2013-01-09 2013-01-10 2013-01-11 2013-01-12 2013-05-23 2013-01-01 2013-01-02 2013-01-03 2013-01-04 2013-01-05 2013-01-06 2013-01-07 2013-01-08 2013-01-09 2013-01-10 2014-01-11 2014-01-12 2014-01-13 2014-01-14 2014-01-15 2014-01-16 2014-01-17 2014-01-18 2015-01-19 2015-01-20 2015-01-21 2015-01-22 2015-01-23 2015-01-24 2015-01-25
SelectRows = strcmp(T1.Var1,'AREQ');
figure
scatter(T1.Var5(SelectRows), T1.Var7(SelectRows))
% xlim([datetime(2013,01,01) datetime(2014,01,01)])
The loop is not necessary.
.
  12 Comments
Sujata Dhar
Sujata Dhar on 2 Sep 2022
It worked. Thank you so much. I have been looking for answers from long. :) :)
Have a wonderful weekend

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!