Filter löschen
Filter löschen

Determining fscanf formatspec.

10 Ansichten (letzte 30 Tage)
aero123
aero123 am 8 Nov. 2020
Kommentiert: Ameer Hamza am 8 Nov. 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 Kommentare
aero123
aero123 am 8 Nov. 2020
Thank you for your last answer. But i want to figure out how to make it with fscanf :(
Thank you by the way.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 8 Nov. 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 Kommentare
aero123
aero123 am 8 Nov. 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 am 8 Nov. 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{:})

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by