How can I run preloaded csv data through a loop?

2 views (last 30 days)
Andrea
Andrea on 15 Sep 2015
Commented: Jae Song on 17 Sep 2015
% define loaded csv data and other variables
IM_1 = csvread('filename1.csv',2,2,[2,2,214,8]);
IM_2 = csvread('filename2.csv',2,2,[2,2,206,8]);
IM_3 = csvread('filename3.csv',2,2,[2,2,186,8]);
edges1 = 0:1:20;
% loop to create subplots of data
for i = 1:3
sorted_IM_i = IM_i(IM_i(:,1)>3 & IM_i(:,1)<5 , :);
hGi = hist(sorted_IM_i(:,2),edges1);
hNGi = hGi/sum(hGi(:));
figure (1); clf;
subplot(8,1,i)
plot(edges1,hNGi,'r');
end
My data won't enter the loop and I'm not sure how to get the IM_1 to enter the loop first so the rest of the data will go through.

Answers (2)

Walter Roberson
Walter Roberson on 15 Sep 2015

Jae Song
Jae Song on 15 Sep 2015
You can put the three variables (IM_1,IM_2,IM_3) into a cell array variable (IM_s) and loop through the cell array variable. Please see the following code example.
% define loaded csv data and other variables
IM_1 = csvread('filename1.csv',2,2,[2,2,214,8]);
IM_2 = csvread('filename2.csv',2,2,[2,2,206,8]);
IM_3 = csvread('filename3.csv',2,2,[2,2,186,8]);
IM_s = {IM_1, IM_2, IM_3};
edges1 = 0:1:20;
% loop to create subplots of data
for i = 1:3
IM_i = IM_s{i} % extract data;for i = 1, IM_1 data
sorted_IM_i = IM_i(IM_i(:,1)>3 & IM_i(:,1)<5 , :);
hGi = hist(sorted_IM_i(:,2),edges1);
hNGi = hGi/sum(hGi(:));
figure (1); clf;
subplot(8,1,i)
plot(edges1,hNGi,'r');
end
  2 Comments
Andrea
Andrea on 17 Sep 2015
The code mentioned above only plots one histogram, instead of 3. Is there a way to fix that?
Jae Song
Jae Song on 17 Sep 2015
Try figure(i); instead of figure(1);clf;

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!