read a matrix from a text file

7 Ansichten (letzte 30 Tage)
Nitish Reddy Kotkur
Nitish Reddy Kotkur am 20 Okt. 2019
Bearbeitet: per isakson am 21 Okt. 2019
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 Kommentare
KALYAN ACHARJYA
KALYAN ACHARJYA am 20 Okt. 2019
Please do attach output1.txt file.
Nitish Reddy Kotkur
Nitish Reddy Kotkur am 20 Okt. 2019
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

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Joe Vinciguerra
Joe Vinciguerra am 21 Okt. 2019
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 am 21 Okt. 2019
A = readcell('output1.txt');
B = str2num(char(replace(split(string([A{:,1}]),']'),...
{'00', '10', '01', '11', '['},{'0 0', '1 0', '0 1', '1 1', ''})));

Community Treasure Hunt

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

Start Hunting!

Translated by