How can I load data from a .csv as a string?

조회 수: 12 (최근 30일)
Will Campbell
Will Campbell 2016년 10월 29일
댓글: Star Strider 2016년 10월 29일
I'm trying to import data from a .csv in two columns. The first column has dates in mm/dd/yyyy format, and the second has regular numbers. I'm trying to plot this dataset, with the dates on the x axis and numbers on the y. The problem is, when I try to import the first column, it treats the "/" marks as mathematical operators, so I am unable to use it as a date. I can't find a way to load the data as a string, and especially not how to plot it. This is the code I have so far.
totalData = load('FED-RXI_US_N_B_UK.csv');
dates=totalData(1:length(totalData),1);
rates=totalData(2:length(totalData),2);
dateNumber=datenum(dates,'mm/dd/yyyy')
Thanks in advance!

채택된 답변

Star Strider
Star Strider 2016년 10월 29일
Use the xlsread function with at least two (and at best all three) outputs:
[totalData,str,raw] = xlsread('FED-RXI_US_N_B_UK.csv');
The ‘str’ variable should have your dates as a cell array of strings that you can use as an argument to datenum to convert them to date numbers. From there, you can do anything with them you want. The ‘raw’ variable has everything as a cell array in case you need to take the data in the file apart yourself.
  댓글 수: 2
Will Campbell
Will Campbell 2016년 10월 29일
Awesome, thank you so much. Just a quick followup - is there any way for the x-axis to display the dates themselves, rather than the point in time from January 0, 2000?
Star Strider
Star Strider 2016년 10월 29일
My pleasure.
Yes. See the datetick function.
If you have R2014b or later, also see datetime, a collection of functions to work with and plot dates and times.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by