How do I read a text file with certain format line by line with each line of different length?

12 views (last 30 days)
I just come across a problem that requires reading a vertex-list weighted map. It is just simply a bunch of lines with each line's number as the order and the rest is in pairs separated by comma. So it looks like this:
1 34,45 56,43 23,34
2 34,56 42,23 33,2 54,23
3 43,22
...
What I want to do is to read each row into an array.For example, arr(3,:)=[43,22] arr(2,:)=[34,56,42,23,33,3,354,23] But I don't know to implement it. Can someone help me with that?
  1 Comment
Cedric
Cedric on 24 Sep 2017
Edited: Cedric on 24 Sep 2017
You may find the following thread interesting:
Note that if you really have commas as decimal separator, you may want to replace them before parsing, e.g.
content = fileread( 'MyData.txt' ) ;
content(content == ',') = '.' ;
data = ...

Sign in to comment.

Accepted Answer

Cedric
Cedric on 24 Sep 2017
Edited: Cedric on 24 Sep 2017
There are more orthodox solutions, but I updated a former answer quickly in case you need a short term solution. The only assumption is that you don't have gaps in the middle of rows, but that missing data are always on the right side of your table.
content = fileread( 'MyData.txt' ) ;
% - Replace decimal separator so SSCANF works.
content(content == ',') = '.' ;
% - Split in rows, remove extra empty ones.
data = strsplit( content, '\n' ) ;
while isempty( data{end} )
data(end) = [] ;
end
% - Convert to numeric and get max number of columns.
data = cellfun( @(x) sscanf( x, '%f' ), data, 'UniformOutput', false ) ;
nCols = max( cellfun( @numel, data )) ;
% - Define padding function and pad with e.g. NaNs.
pad_fun = @(x) [reshape( x, 1, [] ), repelem( NaN, 1, nCols-numel( x ))] ;
data = cellfun( pad_fun, data, 'UniformOutput', false ) ;
% - Concatenate padded rows.
data = vertcat( data{:} ) ;
This pads missing values with NaNs. You can choose padding values but you have to pad if you want to have a numeric array as the output. If not, you can use a cell array of rows, but that limits the computations that you can perform, i.e. you cannot operate on columns or on all (or blocks of) rows simultaneously.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!