필터 지우기
필터 지우기

Read data from text file. Tables with uneven number of rows

조회 수: 1 (최근 30일)
Auwais Ahmed
Auwais Ahmed 2022년 2월 10일
댓글: Turlough Hughes 2022년 2월 11일
I have data of following shape.
I need to save all the numerical values in tables from the file as matrices. The problem is that all the tables have different number of rows. What would be the best way to go about solving the problem. Thanks
  댓글 수: 3
Auwais Ahmed
Auwais Ahmed 2022년 2월 10일
This is the file. Thanks
Benjamin Thompson
Benjamin Thompson 2022년 2월 10일
How do you want the data combined together? If you combine everything into a single table then you will have rows from different trajectories. Is that OK? Can you provide more information about what you want?

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

답변 (1개)

Turlough Hughes
Turlough Hughes 2022년 2월 11일
편집: Turlough Hughes 2022년 2월 11일
fid = fopen('t1.txt');
c = 1;
while ~feof(fid)
d = fgetl(fid);
if ~isempty(regexp(d,'X.*Y.*Z.*Energy', 'once'))
part = textscan(fid,'%f%f%f%f');
data{c} = [part{:}];
c = c+1;
end
end
fclose(fid);
This uses fgetl to find the headers, then textscan reads the arrays.
  댓글 수: 2
Stephen23
Stephen23 2022년 2월 11일
Replace the WHILE and IF with the simpler
while ~feof(fid)
...
end
Turlough Hughes
Turlough Hughes 2022년 2월 11일
Thanks Stephen, I've edited the answer.

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

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by