Importing csv with dates

조회 수: 16 (최근 30일)
D.J
D.J 2018년 8월 25일
댓글: dpb 2018년 8월 27일
Hi all. I am trying to import a csv file which includes numbers and dates using Import data > numeric matrix > Generate Function. All goes well, except for the Dates column. The original format is dd/mm/yyyy, but the imported file replaces all the dates as 1,2,3, etc. Restrictions: -I am not allowed to make any changes to the file before importing. -I tried to import using "column vectors" instead of "Numeric Matrix" but this brings the dates in "datetime" vector instead of "double", which causes me lots of troubles when I try to do other functions and codes, so I am trying to keep everything as "double". Many thanks
  댓글 수: 7
D.J
D.J 2018년 8월 25일
편집: Walter Roberson 2018년 8월 25일
I am not sure I understood your question. I basically need to :
1- import the file,which I did using Import data > numeric matrix > Generate Function.
2-Plot the data showing the max and min temperature for each date. I used the following code (which I am sure is not the most reasonable one, but couldn't come up with any other way:
A= (1:31);
A';
Date=A';
x=A';
y=Tmin_all;
z=Tmax_all;
plot3(x,y,z);
xlim([1 31])
Attached is the plot i came up with. I need all dates to show, that is why I am trying to solve the problem from the beginning,which is importing dates with the correct format, rather than working around it.
I hope this clarifies things.
Many thanks
Walter Roberson
Walter Roberson 2018년 8월 25일
Importing dates with the correct format contradicts your requirement to keep everything double.
When the date 18/07/2017 is imported and you want to get a double out of that, then what value would you want that double to have? Do you want Julian Date? Do you want Excel Date Number (1900)? Do you want Excel Date Number (1904) ? Do you want MATLAB Serial Date Number? Do you want leap years to be handled, or do you want the approximation of fixed length years?
It would be so much easier if you were willing to use datetime objects.

댓글을 달려면 로그인하십시오.

채택된 답변

D.J
D.J 2018년 8월 25일
편집: Walter Roberson 2018년 8월 25일
I tried to do the plot as suggested, I am getting the error Unrecognized variable name 'Date'.
This is what I did:
plot(t.Date,[t.MinimumTemperature__C_ t.MaximumTemperature__C_])
  댓글 수: 11
D.J
D.J 2018년 8월 27일
Many thanks @dpb for the thorough explanation. It is certainly useful to know the history of the function.
dpb
dpb 2018년 8월 27일
It came to me somewhat later that it really is a more subtle error and since my chastisement came out a little more harsh than intended :) that it made some sense to outline the root cause and how it came to be so then seemed a natural extension. I'm sure it will trip up a lot of new users in one form or another with similar not unreasonable expectations that are not met owing to the class difference of the inputs.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

dpb
dpb 2018년 8월 25일
편집: dpb 2018년 8월 25일
t=readtable('test-data.csv');
plot(t.Date,[t.MinimumTemperature__C_ t.MaximumTemperature__C_])
legend('Tmin','Tmax')
ylabel('T, C')
created
datetimes are your friends... :)
And, if you really, really think you must have every day shown on the axis,
hAx=gca;
hAx.XTick=t.Date;
hAx.XAxis.TickLabelFormat='d';
hAx.XAxis.FontSize=8;
after the above produces
Are you still sure you really want to try to convert dates to doubles???
Did I mention datetime is your friend, yet?

D.J
D.J 2018년 8월 25일
You are a gem ! Thank you so much !
  댓글 수: 2
D.J
D.J 2018년 8월 25일
Did I mention that I LOVE datetimes ! :-)
dpb
dpb 2018년 8월 25일
편집: dpb 2018년 8월 25일
No problem, glad to help... :)
There's so much in Matlab it's hard to know where to start to get to end quickest route, granted. But as a general rule, when you start running into issues such as you were, it's generally a good indication you're not using the right approach so stepping back and looking for alternatives is a good idea when that happens instead of just trying to beat Matlab into submission in an arbitrary direction.
The builtin datetime -aware plot routines are a relatively recent introduction to Matlab (prior to that there were datenum and a klunky companion routine to convert the internal representation as doubles to date strings) but it is one thing that has been a real step forward for ease of use for most uses.
The table is another...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Bar Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by