Determining fscanf formatspec.

10 次查看(过去 30 天)
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.

采纳的回答

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