The display of symbolic matrices are not coded as MATLAB code, and are not coded as MuPAD code either. Copying and pasting them as-displayed is a mistake most of the time.
Most of the time what you should do instead is use matlabFunction(), possibly with the 'File' option to write the result to a file. Note that when you use the 'File' option that the optimization option is turned on by default, which results in a series of assignment statements being written that when executed will have the desired effect, but will have a different form.
If your symbolic matrix does not use piecewise() or int(), then if you execute the function generated by matlabFunction, passing in symbolic variables for each of the arguments, then the result will recreate the original matrix -- however rational values might have lost precision. Rational values will definitely lose precision if you copy and paste the text output of a symbolic matrix.
If you need extended precision to be retained, then it is possible to feval(symengine) or evalin(symengine) to call MuPAD's write() to write the matrix to a file as MuPAD commands, which you can later run under MuPAD to recreate the symbolic expression, using the symengine read() command; see https://www.mathworks.com/help/symbolic/read.html If the individual expressions inside the matrix do not exceed the 25000 characters, then you can use
result_cell = arrayfun(@char, TheSymbolicArray, 'uniform', 0);
this will get you a cell array of character vectors that you could then copy using appropriate formatting:
temp = result_cell.';
fmt = [repmat('%s, ...\n', 1, size(result_cell,2)-1), '%s;\n'];
fid = fopen('Output.txt', 'wt');
fprintf(fid, 'TheSymbolicArray = [\n');
fprintf(fid, fmt, result_cell{:});
fprintf(fid, '];\n');
fclose(fid)
But be warned that executing this code will lose precision on rational values.
0 Comments
Sign in to comment.