making a graph of using date as x axis.

My data
I am struggling on making a graph of using date as x axis.
My code is below:
csv_file = fopen('test.csv','r');
formatSpec = '%s%s%s';
C = textscan(csv_file, formatSpec, 'Delimiter', ';');
fclose(csv_file);
%char to double
temp = str2double(C{1,3});
%remove NaN in temp
%double to logical
idx = ~isnan(temp(:,1));
%keep remove nan
FlowTemp = temp(idx,1);
x= C{1,1} <== does not work.
plot(x,FlowTemp)

6 Comments

What do you mean by 'does not work?' Do you get an error message? How is it different than what you expected?
I'm assuming that the command itself works, but that you aren't able to plot the results. This is most likely because x is an array of strings, not of dates and times. You should use datetime() to convert into a proper date and time format.
hello Bob
Thank you so much for your reply.
the error code is:
Error using plot
Invalid data argument.
Error in TsFRTempCSV (line 32)
plot(x,FlowTemp)
It seem x is a cell in this case. therefore, the graph can not show up.
I used datetime() to cover the code, and it showed error again.
the error code is:
Could not recognize the format of the date/time text. You can specify a format character vector using the
'InputFormat' parameter. If the date/time text contain day, month, or time zone names in a language foreign
to the 'en_US' locale, those might not be recognized. You can specify a different locale using the 'Locale'
parameter.
Error in TsFRTempCSV (line 30)
x= datetime(C{1,1});
Ok, so it seems likely that you have another layer of cells within C. I would suggest trying something like the following:
x = datetime([C{1,1}{:}]);
I'm not positive it will work, cells can be tricky like that. If that doesn't work you might try:
x = datetime(cell2str(C{1,1})); % I think cell2str is the command
Hello Bob
Thank you so much again.
It still shows error:
undefined function or variable 'cell2str',
The only thing I can do is cell2table. I type class x, and it shows 'char'.
Is it possible that my .csv file contains header( on the top of column) and then cause those problems?
Could you share the value of C{1,1} (at least the first 5-6 values)?
Hello Adam,
Thank you so much for your reply.
This is the value in cell, the original one. This value type is the same as C{1,1}
Capture.JPG

Sign in to comment.

 Accepted Answer

It's unclear what is stored in C{1,1} so I've provided a few options.
If class(C{1,1}) == 'cell'
%% If class(C{1,1}) == 'cell'
% Produce fake data
C{1,1} = cellstr(datestr(floor(now)-1 : 0.1 : now, 'yyyy-mm-dd HH:MM:ss'));
FlowTemp = rand(size(C{1,1}));
% Plot the time series
plot(datetime(C{1,1}),FlowTemp)
If class(C{1,1}) == 'char'
%% If class(C{1,1}) == 'char'
% Produce fake data
C{1,1} = datestr(floor(now)-1 : 0.1 : now, 'yyyy-mm-dd HH:MM:ss');
FlowTemp = rand(size(C{1,1},1),1);
% Plot the time series
plot(datetime(C{1,1}),FlowTemp)
If neither of these work, please provide the results for these two lines:
class(C{1,1})
size(C{1,1})

4 Comments

Hello Adam
Thank you so much. It plot something.
before I run your code, class(C{1,1}) is cell, size(C{1,1}) is 2118 1.
class O.JPGsize o.JPG
I run your code class(C{1,1})==cell
the amount of the FlowTemp data is cut to 20.
1st.JPG
My code produces fake data. I chose a set of dates starting "yesteday" and going to 'now' every ~24 minutes. The FlowTemp data are just random numbers that are the same size as the date vector. Now you can apply this to your data.
plot(datetime(C{1,1}),FlowTemp)
Dear Adam
Thank you so much for your help.
I really appreciate it.
Glad I could help!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 11 Mar 2019

Commented:

on 12 Mar 2019

Community Treasure Hunt

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

Start Hunting!