複数のテキストデータから一つの散布図の作り方

8 views (last 30 days)
Naoki Ishibashi
Naoki Ishibashi on 27 Sep 2016
Commented: michio on 27 Sep 2016
複数のテキストデータ、TA20040101.txt から TA20041231.txt, を読み込みその値を一つの散布図にプロットしたいのですが以下の自分のプログラムだと1テキストファイルに一つの散布図が作られてしまいます。hold onが問題なんだと思うのですがいい考えがないのでアドバイスいただけると嬉しいです。
for i = 1:12
if i<10
monstr = ['0', num2str(i)];
else
monstr = num2str(i);
end
for j = 1:31
if j<10
daystr = ['0', num2str(j)];
else
daystr = num2str(j);
end
filename = ['TA2004',monstr,daystr,'.txt'];
x = load(filename);
scatterplot(x)
hold on
end
end
よろしくお願いします。
  1 Comment
Teja Muppirala
Teja Muppirala on 27 Sep 2016
ちなみに、 その '0' をつけるかつけないかの処理は sprintfを利用すれば、楽です。(わざわざif/elseを使わなくてもいい)
for i = 1:12
for j = 1:31
filename = sprintf('TA2004%02d%02d.txt',i,j)
end
end

Sign in to comment.

Accepted Answer

Teja Muppirala
Teja Muppirala on 27 Sep 2016
2番目以降のscatterplotに、最初に作成されたscatterplotのfigureハンドルを与えれば、figureが再利用できます。
たとえば:
h = scatterplot(randn(100,2));
hold on;
scatterplot(randn(100,2),[],[],'r.',h);

More Answers (1)

michio
michio on 27 Sep 2016
Communication System Toolboxの関数 scatterplot は実行毎に新しいFigureが作成されます。散布図を新規のFigureではなく、既存のFigureに追加する場合には、
scatterplot(x,n,offset,plotstring,h)
Figureハンドル h を引数に与える構文をhold onと共に使用します。
for i = 1:12
if i<10
monstr = ['0', num2str(i)];
else
monstr = num2str(i);
end
for j = 1:31
if j<10
daystr = ['0', num2str(j)];
else
daystr = num2str(j);
end
filename = ['TA2004',monstr,daystr,'.txt'];
x = load(filename);
if i==1
h = scatterplot(x);
hold on
else
scatterplot(rand(100,1),[],[],[],h);
end
end
end
またはMATLABの scatter 関数を代わりに使用してください。
  2 Comments
Teja Muppirala
Teja Muppirala on 27 Sep 2016
if i==1
h = scatterplot(x);
...
Should be if i==1 && j == 1
michio
michio on 27 Sep 2016
Ah, you are correct. Thanks for pointing that out :)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!