필터 지우기
필터 지우기

Reading in data from text file in a certain format

조회 수: 3 (최근 30일)
J T
J T 2021년 8월 16일
댓글: Star Strider 2021년 8월 16일
Hello, I have a set of text file data that are structured in the following way:
header1
header2
5 columns by N rows (data set 1)
header3
header4
5 columns by M rows (data set 2)
header5
header6
5 columns by K rows (data set 3)
this structure repeats for an arbitrary number of data set, how should I read in these sets of data one by one? I tried
importdata('file.txt')
but it only reads the first set and ignored the rest.
Any help would be appreciated!

채택된 답변

Star Strider
Star Strider 2021년 8월 16일
A similar problem presented itself in Reorganization of experimental data and that solution could be adapted to your problem. It uses textscan because I have used it successfully with similar problems.
So long as the number of columns does not change between the data segments you want to read, adapting it should be straightforward. The number of rows is irrelevant, and the approach adapts to them.
.
  댓글 수: 2
J T
J T 2021년 8월 16일
Hello, I attempted textscan but it still only returns the first set of date and ignored the rest, here is the code I tried:
fidi = fopen('file.txt','rt');
C = textscan(fidi, '%f%f%f%f%f', 'HeaderLines',2,'EmptyValue',0);
M = cell2mat(C);
fclose(fidi);
Star Strider
Star Strider 2021년 8월 16일
In order to read a segmented file, the textscan call needs to be in a loop as in the example code. Also, checking for the empty matrix is essential in order to avoid an infinite loop if the file does not have a valid end-of-file indicator, so all you should need to do is to change the format descriptor and the 'HeaderLines' value for it to work with your file.
.

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by