Putting first line of text files in seperate rows

2 ビュー (過去 30 日間)
Hari krishnan
Hari krishnan 2018 年 8 月 20 日
コメント済み: Hari krishnan 2018 年 8 月 20 日
Hi, i have four text files.I need to open and read first line of each text file and put each of these lines one after another in a vector. In the corresponding code shown, it just puts all the lines in a single row of the vector. Any help will be appreciated.
for ii = 1:number_of_files
varNames = textscan(fileID, format, 1, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'TextType', 'string', 'ReturnOnError', false);
time_start_of_experiment = [varNames{:}];
end
  2 件のコメント
Rik
Rik 2018 年 8 月 20 日
You have the output as a cell, but then you convert it to a row yourself. Also you are overwriting the output every cycle of your loop. Is the varNames correct, but now you need a way to concatenate the result in a cell array?
Hari krishnan
Hari krishnan 2018 年 8 月 20 日
Currently when i get the output, its a string with all the first line of text files added one after the other in a row. What i need is to put the first line from each text file to seperate rows.

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

採用された回答

dpb
dpb 2018 年 8 月 20 日
time_start_of_experiment = cell(number_of_files,1);
for ii = 1:number_of_files
varNames = textscan(fileID, format, 1, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'TextType', 'string', 'ReturnOnError', false);
time_start_of_experiment(ii,1) = varNames;
end
The above snippet doesn't seem correct in that it uses the same fileID for every pass through the loop; that would be reading subsequent records in the same file, not a single record in a sequence of separate files.
That would need an fopen/fclose pair inside the loop surrounding the call to textscan and an incrementing on an array of file names.

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by