problem reading TMY .csv files from MATLAB
Show older comments
Hi, i have a .csv file and i need to call some data from there. It's the kind of file with coma (,) separating each number, it's a typical meteorological year (TMY), wich have 8760 lines and 17 rows.
SThis is some part of what i need to be readed:
1998-1-1-00 27.0 23.1 80.7
1998-1-1-01 26.9 23.1 80.5
1998-1-1-02 26.9 23.0 80.5
1998-1-1-03 26.9 22.8 80.5
1998-1-1-04 27.0 22.8 80.7
1998-1-1-05 26.9 22.7 80.5
The problem i have is that if i use the cvsread command:
tmy=csvread('TMY fecha.csv');
The tmy generated is a matrix that have zeros in places where there wont supose to be. Something like this:
1998 -1 -1 0 27 23.1 80.7 73.7
0 0 0 0 0 0 0 0
1998 -1 -1 -1 26.9 23.1 80.5 73.7
0 0 0 0 0 0 0 0
1998 -1 -1 -2 26.9 23 80.5 73.5
0 0 0 0 0 0 0 0
1998 -1 -1 -3 26.9 22.8 80.5 73.1
0 0 0 0 0 0 0 0
1998 -1 -1 -4 27 22.8 80.7 73.1
0 0 0 0 0 0 0 0
1998 -1 -1 -5 26.9 22.7 80.5 72.9
0 0 0 0 0 0 0 0
With this problem, i've tried with other way to read the file, so i have this:
fid = fopen('TMY fecha.csv','r');
data = textscan(fid,'%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s','Delimiter',',',...
'CollectOutput',true);
fclose(fid);
tmy=data{1};
The tmy generated here is good when you see the matrix, but when you try to call some specific data from there, it´s generated with 'comas'
Something like this:
'1998-1-1-00' '27.0' '23.1' '80.7'
'1998-1-1-01' '26.9' '23.1' '80.5'
'1998-1-1-02' '26.9' '23.0' '80.5'
'1998-1-1-03' '26.9' '22.8' '80.5'
The second problem is having this 'comas' with each number, because i cant call them to be used in mathematical process.
Somebody can help me?
Answers (1)
Walter Roberson
on 21 Feb 2013
csvread() is only for files that are purely numeric.
I do not see any commas in your result from textscan. I do see apostrophes in what you show. However, what you show is not what is actually stored: the apostrophes are not stored and it is just the display format to show you where the ends of each string are. You cannot (easily) do mathematical calculations with strings.
What you probably want is:
tmydates = datevec(tmy(:,1), 'YYYY-MM-DD-HH');
tmyvals = str2double(tmy(:,2:end));
Categories
Find more on Cell Arrays 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!