reading large text files into matrix

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 comentarios

dpb
dpb el 20 de Oct. de 2019
Editada: dpb el 20 de Oct. de 2019
How are you generating the input file? I'd suggest best solution would be to go back and fix that process to not make such a messy file to import.
I'm sure one could do this this way, but why?
ADDENDUM:
I just noticed the last algebraic expression at the end...I'd wager the result didn't import correctly even with shorter rows altho I don't have latest release installed at moment so can't test readmatrix
If you're adamant in going this route which I think is probably more difficult than needs be to get to the end objective (which we don't know precisely what is being done with this so can't judge more than the bare isolated question on syntax), you probably want to create an m-file and incorporate the expression to assign to a variable in the expression as well. Then just execute the m-file and let the interpreter handle the parsing.
Sort out the format for generating it if you can.
Try the following example:
mydata=round(rand(5,40));
save('output_test.txt','mydata','-ascii') % saves to file called output_test.txt
Reading the data back in is then much simplified:
data=textread('output_test.txt')
dpb
dpb el 20 de Oct. de 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 el 20 de Oct. de 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.

Iniciar sesión para comentar.

Respuestas (1)

Stephan
Stephan el 21 de Oct. de 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', ''})));

Preguntada:

el 20 de Oct. de 2019

Respondida:

el 21 de Oct. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by