How do I read a text file with certain format line by line with each line of different length?
14 ビュー (過去 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
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!