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

13 次查看(过去 30 天)
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 个评论
Cedric
Cedric 2017-9-24
编辑:Cedric 2017-9-24
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 = ...

请先登录,再进行评论。

采纳的回答

Cedric
Cedric 2017-9-24
编辑:Cedric 2017-9-24
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by