Read a matrix with symbolic variables

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

 Akzeptierte Antwort

Voss
Voss am 4 Apr. 2024

0 Stimmen

filename = 'matrix.txt';
M = str2sym(split(readlines(filename),','))
M = 

2 Kommentare

Louis
Louis am 4 Apr. 2024
This works great thanks!
Voss
Voss am 4 Apr. 2024
You're welcome!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2022b

Gefragt:

am 4 Apr. 2024

Kommentiert:

am 4 Apr. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by