Fastest way to read mixed-type and position-delimited text files

조회 수: 2 (최근 30일)
Blue
Blue 2020년 5월 7일
댓글: Star Strider 2020년 5월 7일
Hi,
I need to convert mixed-type text files that are position-delimited into cell arrays. To do this, I have a reference file which contains the name of each variable as well as the start and end position of each variable. My problem is that the code below is, of course, far too slow, particularly when comes the time to read files that have 200-300k lines. So. What would be the fastest way to convert mixed-type and position-delimited text files into cell arrays ?
% Mixed-type file
file = {'10036N', '12456J', '56786N', '89006N'}'
% reference file (variable name, start position, end position)
reference_file = {'var1', 1, 1; 'var2', 2, 4; 'var3', 5, 6;}
% empty cell array for output
c = cell(length(file), length(reference_file));
% Loop over each line and extract each variable based on position (obviously very slow)
for j = 1:length(file)
for k = 1:length(reference_file) % start position; stop position
c(j, k) = extractBetween(file(j, 1), reference_file(k, 2), reference_file(k, 3))
end
end
Thank you,

채택된 답변

Star Strider
Star Strider 2020년 5월 7일
If you have R2016b or later, see if the FixedWidthImportOptions function will do what you want. (I have no experience with it, so my ability to help you with it is therefore somewhat limited. If you have problems with it, attach a representative sample file for me to experiment with.) You will probably need to use it with readtable , since that is the only function that FixedWidthImportOptions references in its documentation.
  댓글 수: 2
Blue
Blue 2020년 5월 7일
Huh. I didnt know this existed. Thank you for the tip, I think I am good to go.
Star Strider
Star Strider 2020년 5월 7일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by