Borrar filtros
Borrar filtros

Determining fscanf formatspec.

10 visualizaciones (últimos 30 días)
aero123
aero123 el 8 de Nov. de 2020
Comentada: Ameer Hamza el 8 de Nov. de 2020
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 comentarios
aero123
aero123 el 8 de Nov. de 2020
Thank you for your last answer. But i want to figure out how to make it with fscanf :(
Thank you by the way.

Iniciar sesión para comentar.

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 8 de Nov. de 2020
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 comentarios
aero123
aero123 el 8 de Nov. de 2020
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 el 8 de Nov. de 2020
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{:})

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by