read a matrix from a text file

조회 수: 9 (최근 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 CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by