Error when trying to plot with a datetime array
1 view (last 30 days)
Show older comments
I am trying to seperate the variable mFJ2 into two types based on the two parameter types in column 2 which are {'1'} and {'2'}. After I seperate them into two tables I am attempting to plot the first parameter which I made into an array called flowFJ2 against the array dateFJ2 but it is giving me this error:
Error using plot
Data must be numeric, datetime, duration or an array convertible to double.
So if someone could help me figure out how to fix this that would be great thanks!
Code:
%% Read the data
dataFJ2=readtable('Daily__Jun-20-2021_12_32_15PM.csv');
[a,b]=size(dataFJ2);
%getting the date
dateFJ2=table2array(dataFJ2(:,3));
%getting the parameter values
pFJ2=table2array(dataFJ2(:,2));
pFJ2=categorical(pFJ2);
categories(pFJ2)
%getting the measurement values
mFJ2=dataFJ2(:,4);
%putting into a table
TFJ2=table(pFJ2,dateFJ2,mFJ2);
p1FJ2=(TFJ2.pFJ2 == '1');
Tp1FJ2=TFJ2(p1FJ2,:);
p2FJ2=(TFJ2.pFJ2 == '2');
Tp2FJ2=TFJ2(p2FJ2,:);
dateFJ2=table2array(Tp1FJ2(:,2));
dateFJ2=datetime(dateFJ2, 'Format','yyyy/MM/dd');
flowFJ2=table2array(Tp1FJ2(:,3));
plot(dateFJ2,flowFJ2)
6 Comments
dpb
on 25 Jun 2021
Again, without data to test with or better yet, an actual demo case that can run, we're just guessing what's really going on...
Accepted Answer
dpb
on 26 Jun 2021
tDaily=readtable('Daily__Jun-20-2021_12_32_15PM.csv','NumHeaderLines',1);
tDaily.ID=categorical(tDaily.ID);
tDaily.Date=datetime(tDaily.Date,'InputFormat','yyyy/MM/dd');
tDaily.SYM=categorical(tDaily.SYM);
>> head(tDaily)
ans =
8×5 table
ID PARAM Date Value SYM
_______ _____ ___________ _____ ___
01FJ002 1 01-Mar-1978 0.227 B
01FJ002 1 02-Mar-1978 0.195 B
01FJ002 1 03-Mar-1978 0.15 B
01FJ002 1 04-Mar-1978 0.136 B
01FJ002 1 05-Mar-1978 0.116 B
01FJ002 1 06-Mar-1978 0.108 B
01FJ002 1 07-Mar-1978 0.099 B
01FJ002 1 08-Mar-1978 0.093 B
[g,id]=findgroups(tDaily.PARAM);
for i=1:numel(unique(g))
figure
ix=(g==i);
plot(tDaily.Date(ix),tDaily.Value(ix))
title(compose('Daily Value vs Time PARAM = %d',i))
end
gives two figures.
We're having strong t-storm and my bandwith is so limited couldn't upload the figures themselves...
5 Comments
dpb
on 26 Jun 2021
"Logical addressing"
g is the group number returned from findgroups and so is a vector of 1:nGroups (2 in this case although I wrote the code to be generic instead of hardcoding in the '2', always an objective to not hide "magic numbers" inside code that have to be changed manually when the problem/dataset changes just a little).
So, then the ix array is also a vector, but is either True|False (1|0 but a logical variable, not a number is key here) where the True is where the group number matches the index, False elsewhere.
MATLAB, being vectorized, knows all about vector array indices, be they numeric or logic; if they're numeric they refer to the actual element position in the variable; if they're logical, they return the position in the array for those elements which are True and ignore the False.
It's discussed in the documentation on a section on addressing -- and while it is basic, it is one of most powerful fo the features in using MATLAB effectively.
More Answers (0)
See Also
Categories
Find more on Graphics Object Programming 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!