필터 지우기
필터 지우기

Determining fscanf formatspec.

조회 수: 10 (최근 30일)
aero123
aero123 2020년 11월 8일
댓글: Ameer Hamza 2020년 11월 8일
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 which Formatspec could make them to the matrix i need.
I want to use fscanf.
  댓글 수: 2
aero123
aero123 2020년 11월 8일
Thank you for your last answer. But i want to figure out how to make it with fscanf :(
Thank you by the way.

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

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 11월 8일
Try this code using fscanf()
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)
nls = idx(i)-line_read;
arrayfun(@(x) {fgetl(fid)}, 1:nls);
data{i} = fscanf(fid , '%f %f %f %f', [4, inf]).';
line_read = idx(i) + size(data{i},1)+1;
end
fclose(fid);
idx = cellfun(@isempty, data);
data(idx) = [];
  댓글 수: 2
aero123
aero123 2020년 11월 8일
There are some spaces between datas as you can see below
With your code, only first data set been transformed to the matrix.
How should I correct code to make all data be transformed to the matrix?
Ameer Hamza
Ameer Hamza 2020년 11월 8일
What is the output when you run the code? It returns three matrices for three portions of your data
>> data
data =
1×3 cell array
{117×4 double} {326×4 double} {153×4 double}
If you want just a single matrix then add the following line
data = vertcat(data{:})

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by