フィルターのクリア

Copy the same rows from multiple identical files to a single file

2 ビュー (過去 30 日間)
fredb
fredb 2021 年 3 月 12 日
編集済み: fredb 2021 年 3 月 13 日
Hello,
I have 300 files which have similar structure. I am interested in rows 82-nd and 90-th in each file.
The 82-nd row is:
# span 1 = -0.193, 1.980
The 90-th row is:
# span 9 = 0.000, 557.000
I would like to copy the last number from rows 82 and 90 next to each other, also append those values from each file, like so:
output-file:
1.980 557.00
2.568 687.50
1.158 496.03
............
It would also be fine if I could copy the 82nd and 90th row to new single file and "append" the next files to that single file.
Like so:
# span 1 = -0.193, 1.980
# span 9 = 0.000, 557.000
# span 1 = -0.193, 2.568
# span 9 = 0.000, 687.50
# span 1 = -0.193, 1.158
# span 9 = 0.000, 496.03
....
  2 件のコメント
ANKUR KUMAR
ANKUR KUMAR 2021 年 3 月 12 日
Could you please attach one of your files. It would help us to help you.
fredb
fredb 2021 年 3 月 12 日
Thank you for the comment! I added example file that i have 300.
Last number in row 82 shows maximum depth (or pressure) and last number in row 90 shows how long was the device turned on.

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

採用された回答

ANKUR KUMAR
ANKUR KUMAR 2021 年 3 月 12 日
Sicne you have not attached any sample file, I am using a file having 5 rows, and extracting 2nd and 5th rows from these. Attached these files for your reference.
F=dir('*.txt')
Row = [2,5];
formatSpec = '%1s%5s%2s%2s%12s%11s%s%[^\n\r]';
for kk=1:length(F)
fileID = fopen(F(kk).name,'r');
dataArray = textscan(fileID, formatSpec, 2, 'Delimiter', '', 'WhiteSpace', '', 'HeaderLines', Row(1)-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
A{kk}=cat(2,dataArray{:});
end
out=cat(1,A{:})
%copying the last number in a matrix
mat=reshape(str2double(out(:,6)),2,[])'
%saving the rows in a text file
dlmcell('sample.txt',out)
  4 件のコメント
ANKUR KUMAR
ANKUR KUMAR 2021 年 3 月 12 日
編集済み: ANKUR KUMAR 2021 年 3 月 12 日
I forgot to mention. You will get this function on a file exchange.
fredb
fredb 2021 年 3 月 13 日
編集済み: fredb 2021 年 3 月 13 日
Thank you! Your example is working fine but if I change the row numbers to match with my data file:
Row = [82,90];
MatLab outputs data that are on rows 82 and 83.
What am I doing wrong? I added my data file to my first post. The rows that I want are 82 and 90 in every file:
82-> # span 1 = -0.081, 100.794
90-> # span 9 = 0.000, 1019.250

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by