Effacer les filtres
Effacer les filtres

How can I run preloaded csv data through a loop?

1 vue (au cours des 30 derniers jours)
Andrea
Andrea le 15 Sep 2015
Commenté : Jae Song le 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.

Réponses (2)

Walter Roberson
Walter Roberson le 15 Sep 2015

Jae Song
Jae Song le 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 commentaires
Andrea
Andrea le 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 le 17 Sep 2015
Try figure(i); instead of figure(1);clf;

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by