How can I load data from a .csv as a string?
16 views (last 30 days)
Show older comments
Will Campbell
on 29 Oct 2016
Commented: Star Strider
on 29 Oct 2016
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!
0 Comments
Accepted Answer
Star Strider
on 29 Oct 2016
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 Comments
Star Strider
on 29 Oct 2016
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.
More Answers (0)
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!