Mastiff
Mastiff

Reputation: 2240

How to convert a symbolic expression to MATLAB code?

I have a complicated equation I can't seem to solve elegantly, so I want to use the symbolic solver and then just paste the result into my code. The variables look like this kind of thing:

C=sym('C',[3,3]);

Which results in matrix entries that look like C_1_2, etc.

I can't seem to find an easy way to convert the result into the form C(1,2), which I could actually paste into MATLAB code. Is there a way to do this without manually searching and replacing every possible index combination?

Upvotes: 0

Views: 107

Answers (1)

TWhelan
TWhelan

Reputation: 191

You can use MATLAB to clean it up as a string and then paste into code.

newcodestring = regexprep(oldcodestring, 'C_(\d*)_(\d*)', 'C($1,$2)');

Upvotes: 1

Related Questions