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

조회 수: 8 (최근 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에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by