Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Help reading in TMY3 (.csv files) data into Matlab

Subject: Help reading in TMY3 (.csv files) data into Matlab

From: Matthew

Date: 17 May, 2010 21:57:03

Message: 1 of 3

Hi guys,
It would be greatly appreciated if I could get a quick point in the right direction, I am completely new to matlab, but have experience with C and Java.

I have a program which needs to grab data given a longitude and latitude. With these 2 variable inputs, I will read this CSV file:
http://rredc.nrel.gov/solar/old_data/nsrdb/1991-2005/tmy3/TMY3_StationsMeta.csv
And find the nearest location, returning the file number. What I really need is to get this data into an array in matlab; from there I can figure out how to find the closest location.

Then the same deal with the quirk of choosing which file to parse.
Next would be to parse the file referenced by the above returned file number. The total files (about 1,020 or so) is on my hard drive, but for reference one can be seen here:
http://rredc.nrel.gov/solar/old_data/nsrdb/1991-2005/tmy3/by_USAFN.html
And simply put the data from a few columns into an array in matlab. Again, this would be easy for me to take it from there.

Just need help importing the data into matlab for further manipulation, thanks!

Subject: Help reading in TMY3 (.csv files) data into Matlab

From: Ashish Uthama

Date: 18 May, 2010 13:34:31

Message: 2 of 3

On Mon, 17 May 2010 17:57:03 -0400, Matthew <matthewnorlander@gmail.com>
wrote:

> Hi guys,
> It would be greatly appreciated if I could get a quick point in the
> right direction, I am completely new to matlab, but have experience with
> C and Java. I have a program which needs to grab data given a longitude
> and latitude. With these 2 variable inputs, I will read this CSV file:
> http://rredc.nrel.gov/solar/old_data/nsrdb/1991-2005/tmy3/TMY3_StationsMeta.csv
> And find the nearest location, returning the file number. What I really
> need is to get this data into an array in matlab; from there I can
> figure out how to find the closest location.
>
> Then the same deal with the quirk of choosing which file to parse. Next
> would be to parse the file referenced by the above returned file number.
> The total files (about 1,020 or so) is on my hard drive, but for
> reference one can be seen here:
> http://rredc.nrel.gov/solar/old_data/nsrdb/1991-2005/tmy3/by_USAFN.html
> And simply put the data from a few columns into an array in matlab.
> Again, this would be easy for me to take it from there.
>
> Just need help importing the data into matlab for further manipulation,
> thanks!

1. Look at using CSVREAD, XLSREAD or TEXTSCAN to import the data from the
first CSV.
    If you have Excel, this might be useful too:
http://www.mathworks.com/matlabcentral/fileexchange/19707-xls2struct

2. What do you plan to use to obtain the 'nearest location' ?
    I am not use if euclidean distances are valid for lat/lons.
    (Look up the doc for: 'Arithmetic Operators', 'MIN','Creating Strings
with Concatenation' ...)


>> data= xls2struct('TMY3_StationsMeta.csv');
>> inll = [61.4 -149.07]

inll =

    61.4000 -149.0700

>> dist = (ds.Latitude-inll(1)).^2 + (ds.Longitude-inll(2)).^2; [minD,
>> index] = min(dist);
>> fileName= ['http:....' num2str(data.USAF(index)),'.csv']

fileName =

http:....702740.csv


3. Use URLWRITE if you need to pull it off the web.
    Or create the path to the local file using string concatenation as show
above.


Again, there are quite a lot of options to read in CSV data.
You can start here if CSVREAD etc do not work for you:
http://www.mathworks.com/matlabcentral/fileexchange/?term=csv

Subject: Help reading in TMY3 (.csv files) data into Matlab

From: Matthew

Date: 23 May, 2010 04:21:03

Message: 3 of 3

"Ashish Uthama" <first.last@mathworks.com> wrote in message <op.vcwlbtvsa5ziv5@uthamaa.dhcp.mathworks.com>...
> On Mon, 17 May 2010 17:57:03 -0400, Matthew <matthewnorlander@gmail.com>
> wrote:
>
> > Hi guys,
> > It would be greatly appreciated if I could get a quick point in the
> > right direction, I am completely new to matlab, but have experience with
> > C and Java. I have a program which needs to grab data given a longitude
> > and latitude. With these 2 variable inputs, I will read this CSV file:
> > http://rredc.nrel.gov/solar/old_data/nsrdb/1991-2005/tmy3/TMY3_StationsMeta.csv
> > And find the nearest location, returning the file number. What I really
> > need is to get this data into an array in matlab; from there I can
> > figure out how to find the closest location.
> >
> > Then the same deal with the quirk of choosing which file to parse. Next
> > would be to parse the file referenced by the above returned file number.
> > The total files (about 1,020 or so) is on my hard drive, but for
> > reference one can be seen here:
> > http://rredc.nrel.gov/solar/old_data/nsrdb/1991-2005/tmy3/by_USAFN.html
> > And simply put the data from a few columns into an array in matlab.
> > Again, this would be easy for me to take it from there.
> >
> > Just need help importing the data into matlab for further manipulation,
> > thanks!
>
> 1. Look at using CSVREAD, XLSREAD or TEXTSCAN to import the data from the
> first CSV.
> If you have Excel, this might be useful too:
> http://www.mathworks.com/matlabcentral/fileexchange/19707-xls2struct
>
> 2. What do you plan to use to obtain the 'nearest location' ?
> I am not use if euclidean distances are valid for lat/lons.
> (Look up the doc for: 'Arithmetic Operators', 'MIN','Creating Strings
> with Concatenation' ...)
>
>
> >> data= xls2struct('TMY3_StationsMeta.csv');
> >> inll = [61.4 -149.07]
>
> inll =
>
> 61.4000 -149.0700
>
> >> dist = (ds.Latitude-inll(1)).^2 + (ds.Longitude-inll(2)).^2; [minD,
> >> index] = min(dist);
> >> fileName= ['http:....' num2str(data.USAF(index)),'.csv']
>
> fileName =
>
> http:....702740.csv
>
>
> 3. Use URLWRITE if you need to pull it off the web.
> Or create the path to the local file using string concatenation as show
> above.
>
>
> Again, there are quite a lot of options to read in CSV data.
> You can start here if CSVREAD etc do not work for you:
> http://www.mathworks.com/matlabcentral/fileexchange/?term=csv

Thank you for the reply, this is really helpful. Just for your information, the version of xls2struct you wrote and linked to does not recognize latitude and longitude in the above example you posted, but rather puts NaN.

I found this version works fine:
http://www.mathworks.com/matlabcentral/fileexchange/4104-xls2struct

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us