read a matrix from a text file

7 ビュー (過去 30 日間)
Nitish Reddy Kotkur
Nitish Reddy Kotkur 2019 年 10 月 20 日
編集済み: per isakson 2019 年 10 月 21 日
im trying to read a text file containing matrix
A = readmatrix('output1.txt','Whitespace',' []'); when i execute it
its displaying
NaN NaN
NaN NaN
NaN NaN
NaN NaN
here output1.txt file contains data in the given manner
[[0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1 0 1]
[0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0]
what else changes can i make .
  2 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 10 月 20 日
Please do attach output1.txt file.
Nitish Reddy Kotkur
Nitish Reddy Kotkur 2019 年 10 月 20 日
i have attached the file
BTW this worked fine when every matrix row is in just one line but as size of matrix increases every row is being displaued in two or more lines thats when the problem arised
A = readmatrix('output1.txt','Whitespace',' []');
so what changes can i make to above code

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

回答 (2 件)

Joe Vinciguerra
Joe Vinciguerra 2019 年 10 月 21 日
A = readmatrix('output1.txt'... % filename
,'LineEnding',{']'}... % defines ']' as the end of each row instead of a carriage return
,'Delimiter',{'[',' ','\r','\n'}... % define the remaining non-numeric characters as delimiters
,'ConsecutiveDelimitersRule','join'... % treat consecutive delimiters as one
,'LeadingDelimitersRule','ignore'... % ignore delimiters that start a line
);

Stephan
Stephan 2019 年 10 月 21 日
A = readcell('output1.txt');
B = str2num(char(replace(split(string([A{:,1}]),']'),...
{'00', '10', '01', '11', '['},{'0 0', '1 0', '0 1', '1 1', ''})));

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by