reading large text files into matrix

4 views (last 30 days)
Nitish Reddy Kotkur
Nitish Reddy Kotkur on 20 Oct 2019
Answered: Stephan on 21 Oct 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]]-50*50
what else changes can i make .
BTW it worked fine when matrix size is low i.e when every row of matrix is displayed in single line but as size of matrix increases single row is being displayed in multiple lines thats when problem arised.
i have attached the text file.
can some one modfy this A = readmatrix('output1.txt','Whitespace',' []');
so it can read matrix from the file irrespective of how large it is.
  4 Comments
dpb
dpb on 20 Oct 2019
If going to do it that way, though, might as well just use .mat file format.
It's not clear the "why" behind this scheme so hard to generalize best solution -- but the one started down is probably not it.
Turlough Hughes
Turlough Hughes on 20 Oct 2019
I somehow doubt the data was generated in matlab. My comment is just to demo how easy it is to read data similar to Nitish's if it's formatted beforehand.

Sign in to comment.

Answers (1)

Stephan
Stephan on 21 Oct 2019
As already stated in the other question you asked with the same content, a possible way is:
A = readcell('output1.txt');
B = str2num(char(replace(split(string([A{:,1}]),']'),...
{'00', '10', '01', '11', '['},{'0 0', '1 0', '0 1', '1 1', ''})));

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!