MATLAB: How to read complicated text file

MATLABtextscan

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.

Best Answer

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});
Related Question