フィルターのクリア

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 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 11 月 8 日
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 Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by