Read a matrix with symbolic variables

19 次查看(过去 30 天)
Louis
Louis 2024-4-4,18:18
评论: Voss 2024-4-4,20:15
I am trying to obtain matrices with symbolic variables from .txt files. This is an example of a text file.
-1,x,0,x,0,0,0,x,0,x,0,x,0,x,0,x
0,x-1,0,0,0,0,0,x,0,0,0,x,0,0,0,x
0,0,x-1,0,0,0,0,0,x,0,0,x,0,0,0,x
0,0,0,x-1,0,0,0,x,0,0,0,0,0,x,0,x
0,0,0,0,x-1,0,0,x,0,0,0,0,0,0,x,x
0,0,0,0,0,x-1,0,0,x,0,0,0,0,x,0,x
0,0,0,0,0,0,x-1,0,x,0,0,0,0,0,x,x
0,x,0,0,x,0,0,x-1,0,0,0,x,0,0,x,x
0,0,x,0,0,0,x,0,x-1,0,0,x,0,0,x,x
0,0,0,0,0,0,0,0,0,x-1,0,x,0,x,0,x
0,0,0,0,0,0,0,0,0,0,x-1,x,0,0,x,x
0,0,0,0,0,0,0,0,0,0,0,x-1,0,0,0,x
0,0,x,0,0,0,0,0,x,0,x,0,x-1,0,x,x
0,0,0,0,0,x,0,0,x,x,0,x,0,x-1,0,x
0,0,0,0,0,0,x,0,x,0,x,x,0,0,x-1,x
0,0,x,0,0,0,x,0,x,0,x,0,x,0,x,x-1
When I try with readmatrix, the values with an 'x' get replaced with NaN
When I try with importdata, I get a cell where each value is a string containing a row of the matrix.
Another thing I tried is to copy the matrix in Matlab but when I do with bigger matrices (625x625 for example) Matlab starts lagging a lot.
I would appreciate if anyone has any ideas

采纳的回答

Voss
Voss 2024-4-4,18:29
filename = 'matrix.txt';
M = str2sym(split(readlines(filename),','))
M = 

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by