MATLAB Answers

copy symbolic expession in a matrix form

8 views (last 30 days)
Khaled Rashad
Khaled Rashad on 5 Oct 2017
Edited: Walter Roberson on 5 Oct 2017
How can I copy a very big symbolic matrix. I cannot display in the command window because it is more than 25,000 character and i would like to copy as it is. i mean in a matrix form. Any help?

  0 Comments

Sign in to comment.

Answers (2)

Niels
Niels on 5 Oct 2017
you could write the symbolic variable into file (ex. .txt file) -> open the file -> select all -> copy

  0 Comments

Sign in to comment.


Walter Roberson
Walter Roberson on 5 Oct 2017
Edited: Walter Roberson on 5 Oct 2017
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.'; %transpose is important!
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.

Sign in to answer this question.