How do I read only a specific line while reading a text file in MATLAB ?

48 ビュー (過去 30 日間)
I have a text file that I want to import into MATLAB. However I do not want to read it line by line and instead skip to a specific line number. How do I do it?

採用された回答

MathWorks Support Team
MathWorks Support Team 2016 年 10 月 12 日
Use the following sample code to read only a specific line from a text file into MATLAB:
 
>> % open the file
>> fid=fopen('R2CameraDetectionParam.txt');
>> % set linenum to the desired line number that you want to import
>> linenum = 3;
>> % use '%s' if you want to read in the entire line or use '%f' if you want to read only the first numeric value
>> C = textscan(fid,'%s',1,'delimiter','\n', 'headerlines',linenum-1);
If you wish to read more lines remember to reset the file pointer to the start of the file by using the following command :
>> fseek(fid,0,'bof');
  1 件のコメント
Walter Roberson
Walter Roberson 2016 年 10 月 12 日
Or instead of fseek(fid, 0, 'bof') you can frewind(fid)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDesktop についてさらに検索

製品


リリース

R2013a

Community Treasure Hunt

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

Start Hunting!

Translated by