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

19 vues (au cours des 30 derniers jours)
Will Campbell
Will Campbell le 29 Oct 2016
Commenté : Star Strider le 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!

Réponse acceptée

Star Strider
Star Strider le 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 commentaires
Will Campbell
Will Campbell le 29 Oct 2016
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 le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by