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

 Accepted Answer

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

2 Comments

This works great thanks!
You're welcome!

Sign in to comment.

More Answers (0)

Products

Release

R2022b

Asked:

on 4 Apr 2024

Commented:

on 4 Apr 2024

Community Treasure Hunt

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

Start Hunting!