フィルターのクリア

how to read complicated text file

1 回表示 (過去 30 日間)
Michal Kvasnicka
Michal Kvasnicka 2013 年 9 月 19 日
Hello,
How to read the following "complicated" text file via textscan?
see attached file.dat:
name | multiplicity | pos | rot | excore
------------------------------------------------
a | 2 | 2 3 | 1 | 1
b | 1 | 1 2 3 | 6 | 1
c | 2 | 1 | 6 | 0
...
------------------------------------------------
The number of rows is uknown. The number of integers at column "pos" is variable.

採用された回答

Simon
Simon 2013 年 9 月 19 日
try this
% read file with 5 columns and delimiter '|'
fid = fopen('readin.txt');
FC = textscan(fid, '%s %s %s %s %s', 'delimiter', '|');
fclose(fid);
% postprocessing
% remove blanks from string column
FC(1) = strtrim(FC(1));
% convert numeric columns
for n = 2:5
FC{n} = cellfun(@(x) str2num(x), FC{n}, 'UniformOutput', false);
end
name = FC{1};
multiplicity = cell2mat(FC{2});
pos = FC{3};
rot = cell2mat(FC{4});
excore = cell2mat(FC{5});
  3 件のコメント
Michal Kvasnicka
Michal Kvasnicka 2013 年 9 月 19 日
編集済み: Michal Kvasnicka 2013 年 9 月 19 日
Could be possible to create output variables names from 1st row of text file, too?
Simon
Simon 2013 年 9 月 20 日
If the first row is always the header, no problem:
% read file with 5 columns and delimiter '|'
fid = fopen('readin.txt');
FC = textscan(fid, '%s %s %s %s %s', 'delimiter', '|');
fclose(fid);
% get header
Header = cellfun(@(x) x(1), FC);
% remove header from file contents
FC = cellfun(@(x) x(2:end), FC, 'UniformOutput', false);
% postprocessing
% remove blanks from string column
FC(1) = strtrim(FC(1));
% convert numeric columns
for n = 2:5
FC{n} = cellfun(@(x) str2num(x), FC{n}, 'UniformOutput', false);
end
F1 = FC{1};
F2 = cell2mat(FC{2});
F3 = FC{3};
F4 = cell2mat(FC{4});
F5 = cell2mat(FC{5});
for n = 1:5
eval(sprintf('%s = F%d;', Header{n}, n));
clear(sprintf('F%d', n));
end

サインインしてコメントする。

その他の回答 (2 件)

ES
ES 2013 年 9 月 19 日
use textscan with delimiter '|'
FileObj=fopen(FileName);
CellData=textscan(FileObj, ...
'%s %s %s %s %s %s %s %s %s', 'delimiter', '|');
  1 件のコメント
Michal Kvasnicka
Michal Kvasnicka 2013 年 9 月 19 日
The number of rows is uknown!!! So, number of "%s" is not possible to set in advance.

サインインしてコメントする。


Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 19 日
編集済み: Azzi Abdelmalek 2013 年 9 月 19 日
Use fgetl to read your file then parse the result
fid = fopen('file.txt');
line1 = fgetl(fid);
res={line1};
while ischar(line1)
line1 = fgetl(fid);
res{end+1} =line1
end
fclose(fid);
res(end)=[]
  1 件のコメント
Michal Kvasnicka
Michal Kvasnicka 2013 年 9 月 19 日
編集済み: Michal Kvasnicka 2013 年 9 月 19 日
Thanks, but the parsing of res cell in this method is very painfull :) So, what I neeed is exactly as follows:
after reading the file I need the following outputs:
name = {'a','b','c'}
multiplicity = [2 1 2]
pos = {[2 3], [1 2 3], [1]}
rot = [1 6 6]
excore = [1 1 0 ]

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by