creating a matrix in matlab using a text file

26 Ansichten (letzte 30 Tage)
Nitish Reddy Kotkur
Nitish Reddy Kotkur am 11 Okt. 2019
Bearbeitet: per isakson am 21 Okt. 2019
type output.txt;
A = readmatrix('output.txt');
A
This is the output.txt file
[[1 2 2 0 0]
[2 1 0 0 2]
[2 0 1 2 0]
[0 0 2 0 4]
[0 2 0 4 0]]
A =
NaN 2 2 0 NaN
NaN 1 0 0 NaN
NaN 0 1 2 NaN
NaN 0 2 0 NaN
NaN 2 0 4 NaN
but when i passed it as a input to A the first and last rows are displaying NaN.can someone rectify it.

Akzeptierte Antwort

per isakson
per isakson am 11 Okt. 2019
Bearbeitet: per isakson am 11 Okt. 2019
It's the brackets, [], that confuses Matlab. Try
A = readmatrix( 'output.txt', 'Whitespace',' []' );
I have R2018b so I can't test it.
Your output.txt looks more like the right hand side of an assignment in an m-function. Try
A = [[1 2 2 0 0]
[2 1 0 0 2]
[2 0 1 2 0]
[0 0 2 0 4]
[0 2 0 4 0]]
in the command window (copy and paste).
  3 Kommentare
Jeremy Hughes
Jeremy Hughes am 11 Okt. 2019
Bearbeitet: per isakson am 21 Okt. 2019
Space ( char(32) ) is being used as the delimiter and [] are just being ignored.
I'd reccomend this for most cases where there are other characters since you don't have to specify what to omit.
A = readmatrix('output.txt','TrimNonNumeric',true);
Nitish Reddy Kotkur
Nitish Reddy Kotkur am 20 Okt. 2019
Bearbeitet: per isakson am 21 Okt. 2019
now the ouput.txt is in this 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]
A = readmatrix( 'output.txt', 'Whitespace',' []' );
when i ran this its displaying NaN- Can you help me with this

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by