Extract data from a csv file in Matlab

14 views (last 30 days)
Katerina F
Katerina F on 28 Oct 2020
Answered: Walter Roberson on 28 Oct 2020
Hello,
I am trying to open csv file in Matlab (2020) and extract the data, then put the first column of the data in three columns, year, month day hour.
The data in the csv file attached.
I am using the code below but it is not working. It extracts columns with NaN values. Could you please help me with this? Thanks
fileID = fopen('AberSARAHsolar30deg0deg2012.csv','rt')
C=textscan(fileID,'%s %s %s %s %s %s %s %s ','delimiter',',');
fclose(fileID)
%EXTRACT
time =C{1}(2:end);
Gizero = str2double(C{2}(2:end));
T2m=str2double(C{4}(2:end));
WS10m = str2double(C{5}(2:end));
Githirty =str2double(C{8}(2:end));
%EXTRACT TEXT TO YEAR MONTH DAY HOUR
formatIn ='yyyy-mm-dd hh';
DATETIME=datevec(time,formatIn);
YMDH=DATETIME(:,1:4);
  1 Comment
dpb
dpb on 28 Oct 2020
Use readtable instead...
Convert the time string to datetime

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 28 Oct 2020
formatIn ='yyyymmdd:HHMM';
It is questionable as to whether that is the correct format, however. Each of the entries ends in 11 . It is not impossible that each of them was taken at exactly 11 minutes after each hour, but it would be uncommon.

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!