Clear Filters
Clear Filters

reading a csv file with a multiline header and alphanemeric dataset

2 views (last 30 days)
I have a csv file (e.g. filename.csv) with a multiline, irregular header with a fixed length (17 lines). following this, there are columns of data, that I wish to make numeric array. columns may contain empty cells, and non numeric content, which I would want to replace with zeros in my resulting vector. here is the csv file, with a portion of the header:
"Legend"
"M","Missing"
"E","Estimated"
"NA","Not Available"
"**","Partner data that is not subject to review by the National Climate Archives"
"Date/Time","Year","Month","Day","Time","Data Quality","Temp (°C)","Temp Flag","Dew Point Temp (°C)","Dew Point Temp Flag","Rel Hum (%)","Rel Hum Flag","Wind Dir (10s deg)","Wind Dir Flag","Wind Spd (km/h)","Wind Spd Flag","Visibility (km)","Visibility Flag","Stn Press (kPa)","Stn Press Flag","Hmdx","Hmdx Flag","Wind Chill","Wind Chill Flag","Weather"
"2014-04-01 00:00","2014","04","01","00:00","**","2.4","","1.8","","74","","8","","17","","12.9","","101.16","","","","","","NA"
"2014-04-01 01:00","2014","04","01","01:00","**","2.5","","0.9","","78","","7","","21","","16.1","","101.15","","","","","","NA"
"2014-04-01 02:00","2014","04","01","02:00","**","2.1","","0.3","","84","","7","","18","","16.1","","101.09","","","","","","NA"
"2014-04-01 03:00","2014","04","01","03:00","**","2.3","","0.2","","84","","7","","15","","16.1","","101.06","","","","","","NA"
"2014-04-01 04:00","2014","04","01","04:00","**","3.0","","0.9","","76","","7","","13","","16.1","","101.03","","","","","","NA"
"2014-04-01 05:00","2014","04","01","05:00","**","2.8","","0.7","","78","","8","","17","","16.1","","100.96","","","","","","NA"
"2014-04-01 06:00","2014","04","01","06:00","**","2.7","","0.4","","80","","8","","18","","16.1","","100.93","","","","","","NA"
etc.
The data I would like to access is wind direction and wind speed. This should result in a direction vector of:
8
7
7
7
7
8
8
and a speed vector of:
17
21
18
15
13
17
18
I would ideally like two vector, one with direction and the other with speed. If I could obtain another vector with the time (just the hour value) that would be great as well.
Any help is always appreciated!

Answers (1)

Sara
Sara on 16 Apr 2014
[~,~,a] = xlsread('file.xlsx','M18:M53');
for i = 1:length(a)
if(~isnumeric(a{i}))
a{i}=0;
elseif(isnan(a{i}))
a{i}=0;
end
end
a = cell2mat(a);
  11 Comments
Sara
Sara on 17 Apr 2014
I don't get any error running the code for the cvs file you've posted. Just to make sure, is the matlab script in the same folder of the cvs file?
John
John on 22 Apr 2014
I still had trouble running it, with the same error message, and the files were in the same directory. I ended up using just the syntax:
speed = xlsread('Wind.csv', M18:M350');
This just gave me the numerical values I needed. Thanks for the help Sara! :)

Sign in to comment.

Categories

Find more on File Operations 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!