Looping through csv Data Sets

8 views (last 30 days)
Amanda
Amanda on 9 May 2013
I really need someones expertise. I have a csv file like the following (extra spaces is just to make it 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 and strcmpi. Here is the goal: I need to develop a loop that gets each individual state with the associated data with the state and plot it on one plot, and repeats for each state until finish. So for loop one: Alabama has two data sets, so I need this extracted and plotted. Loop two: Texas has 3 data sets so I need this extracted and plotted. And the process repeats until all the states have been applied.
I really need a solution -- even if you are unsure.
Thanks, Amanda

Accepted Answer

cr
cr on 9 May 2013
Edited: cr on 9 May 2013
Not sure how you want to plot, but in case you want to plot blizzards vs damage for each of the state on a separate figure, here is a way:
x = textscan(fid,'%s %d %*c %d %*c','headerlines',1) % fid corresponds to the data file.
st = x{1,1};
[stunq,idx,idy] = unique(x{1,1});
dmg = x{1,2};
bli = x{1,3};
for i = 1:length(idy)
figure(idy(i)), plot(bli(i),dmg(i),'r.'),hold on
end
  2 Comments
Amanda
Amanda on 9 May 2013
WOW: Thank you so much.
cr
cr on 9 May 2013
Forgot to mention the idy output in unique function. Made the changes.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!