How can I run preloaded csv data through a loop?

2 visualizaciones (últimos 30 días)
Andrea
Andrea el 15 de Sept. de 2015
Comentada: Jae Song el 17 de Sept. de 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.

Respuestas (2)

Walter Roberson
Walter Roberson el 15 de Sept. de 2015

Jae Song
Jae Song el 15 de Sept. de 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 comentarios
Andrea
Andrea el 17 de Sept. de 2015
The code mentioned above only plots one histogram, instead of 3. Is there a way to fix that?
Jae Song
Jae Song el 17 de Sept. de 2015
Try figure(i); instead of figure(1);clf;

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by