Looping through csv Data Sets

조회 수: 4 (최근 30일)
Amanda
Amanda 2013년 5월 9일
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

채택된 답변

cr
cr 2013년 5월 9일
편집: cr 2013년 5월 9일
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
Amanda
Amanda 2013년 5월 9일
WOW: Thank you so much.
cr
cr 2013년 5월 9일
Forgot to mention the idy output in unique function. Made the changes.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Bartlett에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by