I have a directory of (space-delimited) text files, each of which contain meteorological data for a year. The file name has the form AAA_BBB_YYYY.txt. There are no headers in the .txt files – instead this information is given in a README file.
I would like to load all the files using a loop. The data should be stored as separate column vectors, or a structure of column vectors (or if there's a better way, I'm open to suggestions). The main concern is that I want to be able to specify the year that the data represents, and the name of the variables (e.g. location_1977_temperature, met.location.y1977.temperature). I envisage doing this using strings e.g.
>> loc='mlo';>> year='y1977';>> data=[1;2;3];>> met.(loc).(year).temp = datamet = mlo: [1x1 struct]>> met.mlo.y1977.tempans = 1 2 3
So the question is how can I achieve the above when I load the data file?
I've generated a function using the data import tool, but this uses (something like a) string literal to assign the variable names e.g.
%%Allocate imported array to column variable names
metmonth = dataArray{:, 2};
I suppose I could do something like
year='1977';data='month';loc='mlo'; met.(loc).(year).(data)=dataArray{: 2};
Is this the recommended way to achieve what I'm trying to do (i.e. I have to use a structure)?
Best Answer