필터 지우기
필터 지우기

creating a matrix in matlab using a text file

조회 수: 14 (최근 30일)
Nitish Reddy Kotkur
Nitish Reddy Kotkur 2019년 10월 11일
편집: per isakson 2019년 10월 21일
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.

채택된 답변

per isakson
per isakson 2019년 10월 11일
편집: per isakson 2019년 10월 11일
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
Jeremy Hughes
Jeremy Hughes 2019년 10월 11일
편집: per isakson 2019년 10월 21일
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 2019년 10월 20일
편집: per isakson 2019년 10월 21일
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

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by