フィルターのクリア

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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by