reading large text files into matrix

조회 수: 3 (최근 30일)
Nitish Reddy Kotkur
Nitish Reddy Kotkur 2019년 10월 20일
답변: Stephan 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]]-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
dpb
dpb 2019년 10월 20일
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 2019년 10월 20일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Stephan
Stephan 2019년 10월 21일
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', ''})));

카테고리

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