MATLAB: How to save a symbolic equation in a txt file

matrixsymbolicwrite

I have a complex system of equations for the dynamics of a rigid body.
I want to store the resultant matrix in a txt file (or any other format that works well) so that I do not have to compute the symbolic expression inside a numerical solver.
How do I store it in a proper format and then how do I read it?

Best Answer

save('test.mat', 'A', 'a', 'b', 'c');
When you use
syms a
that is equivalent to
a = sym('a');
That calls into the symbolic engine to create a variable named a inside the symbolic engine, and return a reference to the variable. The reference will look something like '_symans_[[32,0,43]]' . That reference will be recorded inside a symbolic object and that object will be assigned to the variable named a at the MATLAB level. The MATLAB level variable named a is not itself something that lives inside the symbolic engine; it is just that '_symans_[[32,0,43]]' that was returned by the symbolic engine.
When symbolic operations are done on a, such as a+1 then the symbolic toolbox pulls out that internal reference _symans_[[32,0,43]] and passes that to the symbolic engine along with the '+1', so it would look something like
evalin(symengine, '_symans_[[32,0,43]]+1')
The symbolic engine looks up _symans_[[32,0,43]] in its tables, find the appropriate expression, does the calculation, and returns a reference to a new symbolic variable, such as '_symans_[[32,0,89]]'
When you construct A from [a b c] then what would happen would be that the symbolic engine would get passed something like
evalin(symengine, '_DOM_LIST([_symans_[[32,0,43]], _symans_[[32,0,89], _symans_[[-7,5,18]]])')
and it would construct the list and return yet another internal reference. Notice that it is not getting passed the names of MATLAB variables, only references to expressions that live in the symbolic engine. A would become a symbolic reference to content that MATLAB does not know about until it asks the symbolic engine for the content.
When you save() A, MATLAB would ask the symbolic engine to return a parse-tree of symbolic expressions that can later be executed inside the symbolic engine to re-create the expression. It does not just ask for a printable version of the expression because in symbolic expressions, what appears to be the same variable is not always the same variable, especially when you get into nested structures.
Then you clear the variables. When you do so, the MATLAB level variables a, b, c stop existing, and information connecting them to (say) _symans_[[32,0,43]] is gone. You load symbolic variable A and it recreates references inside the symbolic engine to symbolic variables a, b, c that live in the symbolic engine, and A would get a new _symans reference. But that does not recreate the connection between the MATLAB variables a, b, c and the symbolic engine.
The situation is much like if you had done
a = 1; b = 2; c = 3;
A = [a, b, c];
and saved A and cleared the workspace and restored A, and if you then expected that a and b and c were also automatically restored, on the basis that A referred to them. No, A does not refer to them, MATLAB copied their values into A and throws away the information about where it got the values. Likewise when you have symbolic variables installed, MATLAB copies their values -- but the values of symbolic variables are the references into the symbolic engine, not the connection between the local variables of that name and whatever is inside the symbolic engine.
Consider for example if you had done
syms a b c
A = [a*2;b*0;c+5];
save('test.mat', 'A');
then upon the load() would you expect a and b and c to be restored? A doesn't even contain a reference to symbolic engine variable b -- the b*0 is 0 before you even get around to constructing A. MATLAB does not take a record of the variable names used to construct an expression and the expression that was used, only a copy of the evaluated values.