matlab - Matching rows in CSV data sets -
in matlab, need someones expertise. have csv file following (extra spaces make readable):
state, damage, blizzards, texas, 2, 2, alabama, 1, 0, alabama, 0, 1, texas, 5, 3, montana, 0, 8, arizona, 0, 0, arizona, 0, 1, texas, 8, 5,
i have applied textread , strcmpi. here goal: need develop loop gets each individual state associated data state , plot on 1 plot, , repeats each state until finish. loop one: alabama has 2 data sets, need extracted , plotted. loop two: texas has 3 data sets need extracted , plotted. , process repeats until states have been applied.
here code:
filename = 'datacollect.csv' [state,damage,blizzards] = ... textread(filename,'%s %d... %d','delimiter',',','headerlines',1); index1 = strcmpi(state, 'texas'); damage = damage(index1) blizzards = blizzards(index1) plot(damage,blizzards) %for texas
trying make loop, automatic, don't have hard code it.
i need solution -- if unsure.
amro's answer should point in right direction, here's full solution spelt out you, in case you're still having trouble:
%// parse csv file [states, damage, blizzards] = textread(csvfilename, '%s %d %d', ... 'delimiter', ',', 'headerlines', 1); %// parse data , store in array of structs [u, ix, iu] = unique(states); %// find unique state names s = struct('state', u); %// create struct each state k = 1:numel(u) idx = (iu == k); %// indices of rows matching current state s(k).damage = damage(idx); %// add damage information s(k).blizzards = blizzards(idx); %// add blizards information end
the resulting s
array of structs fields state
, damage
, blizzards
.
now can loop on array of structs, accessing fields of corresponding struct in each iteration. instance, access damage
values of alabama (the second struct), can s(2).damage
.
hope helps!
Comments
Post a Comment