MATLAB でテキストファイルを読み込む際、特定の行のみ読み込むことはできますか?
20 views (last 30 days)
Show older comments
MathWorks Support Team
on 21 Oct 2016
Answered: MathWorks Support Team
on 21 Oct 2016
テキストファイルのデータをMATLAB に読み込みたいと思います。このとき、一行ずつ読み込まず、特定の行のみ読み込みたいです。
Accepted Answer
MathWorks Support Team
on 21 Oct 2016
低水準ファイルI/O関数の textscan 関数で、'headerlines' プロパティで、読み飛ばしの行数を指定することが可能です。
例:
% ファイルオープン
fid=fopen('R2CameraDetectionParam.txt');
% 読み込みたい行番号を定義
linenum = 3;
% データの読み込み
%(一行をまとめて文字列で読み込む場合は、%s, 最初の数値のみを読み込む場合は %f を使用)
C = textscan(fid,'%s',1,'delimiter','\n', 'headerlines',linenum-1);
また、別の行も読みたい場合は、fseek を使用して、ファイルの先頭にファイルポインタをリセットすることができます。
>> fseek(fid,0,'bof')
読み込みの最後には、fclose にてファイルをクローズしてください。
>> fclose(fid);
0 Comments
More Answers (0)
See Also
Categories
Find more on テキスト ファイル in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!