Clear Filters
Clear Filters

Sorting imported data into a structure array

6 views (last 30 days)
Hi everyone-
I have a question about organizing some data I've collected and I couldn't find anything similar on here. Here is a simplified version of my problem:
I have a structure array (named 'hydro'). This structure is for three research sites ('WAU_Forest', 'WAU_Wetland', and 'CHE_Forest'). At each one of these sites, there is a structure for each of the three species of plants (different at each site). For example, hydro(1).spec(1) would be a Hickory at WAU_Forest. For each plant, I'm measuring temperature and transpiration. Now, let's say I have collected some data that's in .csv format:
Site,Species,Temp,Transpiration
WAU_Wetland,Cattail,20,100
WAU_Wetland,Cattail,25,150
WAU_Forest,Hickory,20,300
WAU_Forest,Hickory,20,350
CHE_Forest,Hickory,30,450
CHE_Forest,Boxelder,30,400
CHE_Forest,Hickory,32,200
etc.
My goal is to have MATLAB open a file, extract the data, use the first two columns (site and species) to sort the data, then put the last two columns (temp and transp) into a data table at that section of the structure.
It ends up being a few thousand data points in a couple dozen Excel files - some of which have all the sites, some of which have only one or two - so I'd rather not do this by hand. And, there's actually 15 sites, and a lot more data collected than just temperature and transpiration. I know how to import the data, but sorting it based on the text of the first two columns has me stumped. Any help would be much appreciated.
Thanks in advance for your help,
-Sam
  1 Comment
Hossein
Hossein on 11 Oct 2011
let say that you put all of your excel files into one file or you just run the program for one file and then resume it to the next file. then
for i=1:number_of_rows_in_excel_file
switch z(i,1)
case 'WAU_Wetland'
if strcmp(z(i,2),'Hickory')
hydro(1).spec(1).Temp(k)=z(i,3);
hydro(1).spec(1).Transpiration(k)=z(i,4);
k=k+1;
if strcmp (z(i,2),'Cattail'
hydro(1).spec(2)
case 'CHE_Forest'
if strcmp(z(i,2),'Cattail')
hydro(2).spec(2)
and ...
I am sure you could come up with a more efficient code, but I think all you need is to compare first two column with a strings defining sites and species. Instead of index k you will probably need 9 index so it could be as well k(i,j).
-Hossein

Sign in to comment.

Answers (2)

Fangjun Jiang
Fangjun Jiang on 11 Oct 2011
I don't fully understand the structure of your struct array, but for sorting, you can reference the following example:
>> [Num,Txt]=xlsread('test.csv')
Num =
20 100
25 150
20 300
20 350
30 450
30 400
32 200
Txt =
'DataWAU_Wetland' 'Cattail'
'WAU_Wetland' 'Cattail'
'WAU_Forest' 'Hickory'
'WAU_Forest' 'Hickory'
'CHE_Forest' 'Hickory'
'CHE_Forest' 'Boxelder'
'CHE_Forest' 'Hickory'
>> [SortedTxt,index]=sortrows(Txt)
SortedTxt =
'CHE_Forest' 'Boxelder'
'CHE_Forest' 'Hickory'
'CHE_Forest' 'Hickory'
'DataWAU_Wetland' 'Cattail'
'WAU_Forest' 'Hickory'
'WAU_Forest' 'Hickory'
'WAU_Wetland' 'Cattail'
index =
6
5
7
1
3
4
2
>> SortedNum=Num(index,:)
SortedNum =
30 400
30 450
32 200
20 100
20 300
20 350
25 150

Sam Zipper
Sam Zipper on 12 Oct 2011
Thanks both of you - I will try these out and see what works best for me.

Categories

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