I'm trying to create a loop in which i can extract 4 columns and multiple rows from 18 difference txt files and save the extracted data in one file. Is this possible? This is my code from one file

조회 수: 1 (최근 30일)
fid1 = fopen('filename');
loopdata = textscan(fid1, '%f %f %f %f', 'delimiter',',','headerlines', 86);
fclose(fid1);
loopdata
field = loopdata{1};
moment = loopdata{2};
fieldAdjusted = loopdata{3};
momentAdjusted = loopdata{4};

답변 (1개)

George
George 2016년 10월 26일
Untested, but this should basically work. Get an array of filenames with ls
files = ls('*.txt');
for ii=1:numel(files)
fid1 = fopen(files(ii));
loopdata = textscan(fid1, '%f %f %f %f', 'delimiter',',','headerlines', 86);
fclose(fid1);
% change this to saving data in arrays, this replaces everything every time
field = loopdata{1};
moment = loopdata{2};
fieldAdjusted = loopdata{3};
momentAdjusted = loopdata{4};
end
T = table(field, moment, fieldAdjusted, momentAdjusted);
writetable(T, 'mycombinedfile.txt');
If the files are very large you can look into datastore

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by