数値データ間に複数の列ヘッダが含まれるテキストファイルを読み込むにはどうすればよいですか?
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
以下に示すようなテキストファイルから、列ヘッダ(文字列)を除く全ての数値データを取得するにはどうすればよいですか?
H1 H2 H3 H4
1 2 3 4
5 6 7 8
H5 H6 H7 H8
9 10 11 12
13 14 15 16
17 18 19 20
Akzeptierte Antwort
MathWorks Support Team
am 29 Jun. 2009
次の 2 つの方法が考えられます。
1. importdata 関数を使用する方法
2. textscan 関数を使用する方法
以下、テキストファイル名を mydata.txt として実行例を示します。
1. importdata 関数を使用する方法
importdata 関数は、数値データは、double 型で読み込みます。
>> d = importdata('mydata.txt')
d =
data: [2x4 double]
textdata: {'H1' 'H2' 'H3' 'H4'}
colheaders: {'H1' 'H2' 'H3' 'H4'}
2. textscan 関数を使用する方法
textscan 関数では、読み込み時の数値フォーマットの指定が可能です。
fid = fopen('mydata.txt', 'r');
d = {};
while ~feof(fid)
fgetl(fid); % 列ヘッダを読み込み
c = textscan(fid, '%d %d %d %d'); % 4列のデータをint32型で読み込む
d = vertcat(d, c); % もしくは d = [d; c];
end;
fclose(fid);
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu テキスト ファイル finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!