How to read only the first line/row in a text file?

16 ビュー (過去 30 日間)
kk1991
kk1991 2017 年 4 月 8 日
コメント済み: kk1991 2017 年 4 月 8 日
This is my script. This script read through all columns and rows in the file. I want the script to only read the firs line/row in the text file.
while ischar(tline)
ii = ii + 1;
tmp = textscan(tline, '%s ', 'delimiter', ' ', 'MultipleDelimsAsOne', 1);
tmp = tmp{1};
t(ii) = str2num(tmp{1});
x(ii) = str2num(tmp{2});
tline = fgetl(fid);
end

採用された回答

Geoff Hayes
Geoff Hayes 2017 年 4 月 8 日
kk1991 - if you only want to read the first line in the file, then don't use the while loop or use break to exit the loop once the first line has been read. Or do you mean something else by I want the script to only read the firs line/row in the text file.
For example, your code could become just
tmp = textscan(tline, '%s ', 'delimiter', ' ', 'MultipleDelimsAsOne', 1);
tmp = tmp{1};
t = str2num(tmp{1});
x = str2num(tmp{2});
where you initialize tline as before.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by