reading a huge matrix which is split to several parts

1 回表示 (過去 30 日間)
Jan Pavlik
Jan Pavlik 2020 年 8 月 21 日
コメント済み: Jan Pavlik 2020 年 8 月 24 日
Dear all,
is there a simple way in MATLAB to read a matrix from a text file, when the matrix is too wide to span the linewidth and is therefore split to consecutive parts? An example file is attached.
I will be thankful for any advices.
Jan
  1 件のコメント
KSSV
KSSV 2020 年 8 月 21 日
How about importdata, textscan. ?

サインインしてコメントする。

採用された回答

per isakson
per isakson 2020 年 8 月 22 日
編集済み: per isakson 2020 年 8 月 22 日
The text file contains column and row numbers together with blocks of the matrix. These row and column numbers can be used for checks and to find the number of columns of each block. In my script below, I assume that all the blocks contains four columns. And there is no error checking in my script.
%%
chr = fileread('matrixsample.txt');
chr = strrep( chr, char(13), '' ); % simpler without carrige return
str = string( chr );
%%
[ blocks, matches ] = strsplit( str, '(?m)^[\d\x20]+$' ...
, 'DelimiterType','RegularExpression' );
blocks(1) = []; % first line is a delimiter
%%
cac = cell( 1, numel(blocks) );
for bb = 1 : numel(blocks)
cac(bb) = textscan( blocks(bb), '%*d%f%f%f%f', 'CollectOutput',true );
end
num = cell2mat( cac );
The result is in num
>> whos num
Name Size Bytes Class Attributes
num 8x8 512 double
  1 件のコメント
Jan Pavlik
Jan Pavlik 2020 年 8 月 24 日
Dear per isakson, thank you very much, this indeed works :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by