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

5 Ansichten (letzte 30 Tage)
Naoki Ishibashi
Naoki Ishibashi am 27 Sep. 2016
Kommentiert: michio am 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 Kommentar
Teja Muppirala
Teja Muppirala am 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Weitere Antworten (1)

michio
michio am 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 Kommentare
Teja Muppirala
Teja Muppirala am 27 Sep. 2016
if i==1
h = scatterplot(x);
...
Should be if i==1 && j == 1
michio
michio am 27 Sep. 2016
Ah, you are correct. Thanks for pointing that out :)

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by