필터 지우기
필터 지우기

How can I run preloaded csv data through a loop?

조회 수: 1 (최근 30일)
Andrea
Andrea 2015년 9월 15일
댓글: Jae Song 2015년 9월 17일
% 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.

답변 (2개)

Walter Roberson
Walter Roberson 2015년 9월 15일

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

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by