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

 Respuesta aceptada

Voss
Voss el 4 de Abr. de 2024
filename = 'matrix.txt';
M = str2sym(split(readlines(filename),','))
M = 

2 comentarios

Louis
Louis el 4 de Abr. de 2024
This works great thanks!
Voss
Voss el 4 de Abr. de 2024
You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2022b

Preguntada:

el 4 de Abr. de 2024

Comentada:

el 4 de Abr. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by