複数のテキストファイ​ルからヒストグマムを​作る方法を教えてくだ​さい

1000以上のtest fileをmatlab上にimportするとこまではできたのですが、そのデータを一つのヒストグラムにまとめる方法がわかりません。"hold"を使うと並列にグラフが重なってしまい、縦に各データを積み重ねて一つのヒストグラムを作りたいのですが何かアドバイスいただければ嬉しいです

1 个评论

michio
michio 2016-9-14
参考:それぞれのファイルからのヒストグラムを積み重ねるには一例として
https://jp.mathworks.com/matlabcentral/answers/302703-how-to-plot-a-stacked-histogram-from-multi-text-files
で紹介するbar関数を使う方法があります。

请先登录,再进行评论。

 采纳的回答

Naoki Ishibashi
Naoki Ishibashi 2016-9-12

0 个投票

ありがとうございます。 それでやってみたところまた別のところでエラーが出てしまったのですがアドバイスいただけたら幸いです。
for i = 1:31
if i<10
daystr = ['0', num2str(i)];
else
daystr = num2str(i);
end
for k = 0:7
j = 3*k;
if j<10
hour = ['0', num2str(j)];
else
hour = num2str(j);
end
filename = ['TS2004.07.',daystr,'.',hour,'00.txt'];
x = hist(filename);
[counts,edges] = histcounts(x);
center = (edges(1:end-1)+edges(2:end))/2;
bar(center,counts,1)
end
end
error:
Attempt to execute SCRIPT hist as a function:
/Users/naoki/Documents/MATLAB/Add-Ons/TS2004.07/hist.m
Error in untitled5 (line 15)
x = hist(filename);

10 个评论

michio
michio 2016-9-12
/Users/naoki/Documents/MATLAB/Add-Ons/TS2004.07/hist.m で表示されている hist.m に何らかの要因がありそうです。hist.m が関数として定義されておらず、スクリプトである、というエラーメッセージに見受けられますが、確認をお願い致します。
また、
center = (edges(1:end-1)+edges(2:end))/2;
bar(center,counts,1)
は forループ内で実行するとおそらく意図する挙動とならないと思います。「すべてのデータからのcountsだけを足しあわせて bar関数を実行する」ためには、例えば・・
nbins = 12;
total_counts = zeros(1,nbins);
for i = 1:31
if i<10
daystr = ['0', num2str(i)];
else
daystr = num2str(i);
end
for k = 0:7
j = 3*k;
if j<10
hour = ['0', num2str(j)];
else
hour = num2str(j);
end
filename = ['TS2004.07.',daystr,'.',hour,'00.txt'];
x = hist(filename);
[counts,edges] = histcounts(x,nbins);
total_counts = total_counts + counts;
end
end
center = (edges(1:end-1)+edges(2:end))/2;
bar(center,total_counts,1)
と、各ファイルからの得られる各ビンのデータ数をすべて足しあわせて(total_counts) 、最後に bar を実行します。nbins でビンの数を指定してください。ファイルが無いので動作確認できていませんが、イメージだけでも受け取って頂けると幸いです。
michio
michio 2016-9-12
编辑:michio 2016-9-12
余談ですが、投稿された Answer へのコメント・追記はページ下部の [Answer this question]からではなく、各Answerの'Comment on this Answer' をクリックして投稿して頂ますようよろしくお願いします。
ご丁寧にご回答頂き本当にありがとうございます。 上記のプログラムを試したところ
Error using hist (line 48)
Input arguments must be numeric.
Error in myhistplot (line 17)
x = hist(filename);
とおそらく以下のプログラム部分に問題があり
filename = ['TS2004.07.',daystr,'.',hour,'00.txt'];
x = hist(filename);
text file名はTS2004.07.01.0000.txtからTS2004.07.31.2100.txtです おそらくfile名は間違っていないかなと思うのと、もし間違っている場合 not found のようにエラーが返ってくるかなと思います。 そのため何がエラーを発生させているのかわからない状況で、もし何かアドバイスいただければ幸いです。
x = hist(filename);
と関数 hist を使用されていますが、これは独自に定義された関数でしょうか、それとも MATLAB の hist 関数 (<http://jp.mathworks.com/help/matlab/ref/hist.html>) の使用を意図したものでしょうか。
1つ前のエラーメッセージでは
Attempt to execute SCRIPT hist as a function:
/Users/naoki/Documents/MATLAB/Add-Ons/TS2004.07/hist.m
Error in untitled5 (line 15)
x = hist(filename);
とでており、/Users/naoki/Documents/MATLAB/Add-Ons/TS2004.07/hist.m に存在するファイルで独自に定義された関数が使用されていることが読み取れますが、今回のエラーでは Input arguments must be numeric とあるとおり、数値データを受け取る MATLABで定義される hist 関数を使用されているようにも見受けられます。
x = hist(filename);
との関数の呼び出し方からは、MATLAB で定義されるヒストグラムを描く hist 関数ではなく、filename で指定されるテキストファイルからデータを読み取ることを意図しているのでは、、と推測しているのですがいかがでしょうか?
独自にファイルからデータを読み取る hist 関数を作成されていらっしゃいますでしょうか?
Naoki Ishibashi
Naoki Ishibashi 2016-9-12
MATLAB の hist 関数 (<http://jp.mathworks.com/help/matlab/ref/hist.html>) を使用しているつもりでした。
hist部分を除きいろいろ試してみたのですがうまくいかず再度アドバイスいただけると嬉しいです
nbins = 12;
total_counts = zeros(1,nbins);
for i = 1:31
if i<10
daystr = ['0', num2str(i)];
else
daystr = num2str(i);
end
for k = 0:7
j = 3*k;
if j<10
hour = ['0', num2str(j)];
else
hour = num2str(j);
end
filename = ['TS2004_07_',daystr,'_',hour,'00'];
[counts,edges] = histcounts(filename,nbins);
total_counts = total_counts + counts;
end
end
center = (edges(1:end-1)+edges(2:end))/2;
bar(center,total_counts,1)
error: Error using histcounts
Expected input number 1, x, to be one of these types:
double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64,
logical
Instead its type was char.
Error in histcounts (line 96)
validateattributes(x,{'numeric','logical'},{'real'}, mfilename, 'x', 1)
Error in myhistplot (line 17)
[counts,edges] = histcounts(filename,nbins);
michio
michio 2016-9-13
编辑:michio 2016-9-13
MATLABで定義済みの関数 hist/histogram/histcountsなどは基本的には数値データを入力として受け取りますので、
filename = ['TS2004_07_',daystr,'_',hour,'00'];
[counts,edges] = histcounts(filename,nbins);
とファイル名を直接指定してデータを読み込むことはできません。ですので、まずファイルからヒストグラムの元となるデータを読み込む関数を作成するのが次のステップとなります。
ファイルにデータがどのように保存されているかなどデータの形式に依存しますが、下記のURLを参考にデータを読み込む関数を作成してください。
具体的な操作方法も合わせて紹介した下記ビデオもおすすめです。
上記を参考に、例えば importdataFromFile.m という、ファイル名を指定してデータを読み込む関数が完成すれば、下記のスクリプト実行ですべてのファイルを読み込んで、全部のデータのヒストグラムが描けるはずです。(下記スクリプトは importdataFromFile.m を作って頂かないと実行できませんのでご注意ください。)
nbins = 12;
total_counts = zeros(1,nbins);
for i = 1:31
if i<10
daystr = ['0', num2str(i)];
else
daystr = num2str(i);
end
for k = 0:7
j = 3*k;
if j<10
hour = ['0', num2str(j)];
else
hour = num2str(j);
end
filename = ['TS2004_07_',daystr,'_',hour,'00'];
x = importdataFromFile(finename);
[counts,edges] = histcounts(x,nbins);
total_counts = total_counts + counts;
end
end
center = (edges(1:end-1)+edges(2:end))/2;
bar(center,total_counts,1)
Naoki Ishibashi
Naoki Ishibashi 2016-9-13
I can make histogram. I really appreciate for you support.
michio
michio 2016-9-13
I'm glad it helped :)

请先登录,再进行评论。

更多回答(1 个)

michio
michio 2016-9-11

2 个投票

1000以上のtext fileにあるデータを全てまとめて1つのヒストグラムを作成されたいとのこと、これはそれぞれのファイルからのデータを縦に異なる色で重ねたヒストグラムをイメージすればいいでしょうか?例:
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
bar(y,'stacked')
それとも単にすべてのファイルからのデータをまとめた単色のヒストグラムでしょうか。
後者の場合、一旦すべてのデータを1つの変数にまとめた後にヒストグラムを描くことをお勧めいたしますが、データが多く1つの変数にまとめるのが難しい場合は histcounts 関数を使う方法はいかがでしょうか。http://jp.mathworks.com/help/matlab/ref/histcounts.html
各ファイルからのデータに対して histcounts 関数で各ビンのカウント数だけを計算しておき、最後にまとめて bar 関数でヒストグラムを描きます。下記は1つのデータ x に対して実行した例ですが、すべてのデータからのcountsだけを足しあわせて bar関数を実行すれば、すべてのデータのヒストグラムとなります。
x = randn(10000,1);
[counts,edges] = histcounts(x);
center = (edges(1:end-1)+edges(2:end))/2;
bar(center,counts,1)

1 个评论

michio
michio 2016-9-11
histcounts関数はR2014bで導入された関数です。それ以前のバージョンをご利用の場合には、hist/histcで代用可能です。

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Text Data Preparation 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by