필터 지우기
필터 지우기

Unable to determine formatSpec while reading .txt file by fscanf

조회 수: 4 (최근 30일)
aero123
aero123 2020년 11월 4일
답변: Ameer Hamza 2020년 11월 4일
Hi, i'm trying to make [n , 4] table with the a.txt
I need only need numeric datas but i have no idea which formatSpec i need to use.
I have no idea how to deal with the space blanks between numbers i need.
a = fopen('a.txt' ,'r')
data = fscanf(a , FromatSpec , [4 inf])
Please let me know which formatSpec i need to use.
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 11월 4일
textscan with headerlines set to about 9 or 10 (not sure from my phone)

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

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 11월 4일
This is one way:
lines = strsplit(fileread('a.txt'), '\n', 'CollapseDelimiters', false);
idx = find(contains(lines, 'Global Statistics'));
idx = [9 idx+10];
data = cell(1, numel(idx));
fid = fopen('a.txt' ,'r');
line_read = 0;
for i = 1:numel(idx)
data{i} = textscan(fid , '%f %f %f %f', 'HeaderLines', idx(i)-line_read);
line_read = idx(i) + numel(data{i}{1})+1;
end
fclose(fid);
[data, idx] = cellfun(@(x) deal({[x{:}]}, isempty(x{1})), data);
data(idx) = [];

추가 답변 (0개)

카테고리

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