Error when trying to plot with a datetime array

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 Commenti

Your code would be easier to read if you got rid of the table2array calls and switched to {} indexing
table2array(T(:, 2))
becomes
T{:, 2}
dpb
dpb il 25 Giu 2021
Modificato: dpb il 25 Giu 2021
Not to mention formatting the code in the window -- I did for OP this time..
And. per usual, without the actual data to see what is actually contained in the file and how MATLAB interpreted it, we're shooting in the dark...
Also, though, if the above
dateFJ2=datetime(dateFJ2, 'Format','yyyy/MM/dd');
line works without error, then probably what is happening is you have already plotted onto an axes with an "ordinary" variable as the x-axis variable and that has created a NumericRuler axis -- and you can't plot a datetime on an existing numeric axes; at least directly.
>> plot(rand(10,1))
>> hold on
>> plot(tmp.DateTime,tmp.x_p_)
Data inputs must match the axis configuration. A numeric axis must have numeric data inputs or data inputs which can be converted to double.
>>
Isn't quite the identical message; that may depend on the release being used or precise sequence used. Above tried to put a datetime on a numericRuler axes -- it did have hold on to force the attempt on the same axis that isn't explicitly in you code, but perhaps there's more going on than you've yet posted...
Is why it's so much more helpful to have actual test cases that can run to try to duplicate the error...otherwise, it's having to make up stuff and hope to match the conditions. Sometimes that's easy enough; other times "not so much!".
JMG
JMG il 25 Giu 2021
Modificato: JMG il 25 Giu 2021
Hello! Thanks for the help, sorry I am new to using the MATLAB answers so I keep forgetting to add in my file that I am working with. Its attached now.
I tried plotting each variable individually against an array of randomly generated numbers. The dateFJ2 array worked butwhen I did it with the flowFJ2 it showed me the same error
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...
I linked my data in the comment above. Here it is again in case my other comment didnt load. Thanks

Accedi per commentare.

 Risposta accettata

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 Commenti

I did still have the second one and it managed to do it, eventually...
Thank you! I got it to work as well. I understand your code for the most part but I was wondering if you could explain the part of the code that says:
ix=(g==i);
and how that works within this part:
plot(tDaily.Date(ix),tDaily.Value(ix))
Sorry if this is basic knowledge I am new to MATLAB and learning as I go.
"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.
Okay that makes sense thank you very much

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Release

R2021a

Richiesto:

JMG
il 25 Giu 2021

Commentato:

JMG
il 26 Giu 2021

Community Treasure Hunt

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

Start Hunting!

Translated by