Error when trying to plot with a datetime array
Mostra commenti meno recenti
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
Walter Roberson
il 25 Giu 2021
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}
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
il 25 Giu 2021
dpb
il 25 Giu 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...
JMG
il 25 Giu 2021
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Dates and Time in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
