copy symbolic expession in a matrix form

조회 수: 8 (최근 30일)
Khaled Rashad
Khaled Rashad 2017년 10월 5일
편집: Walter Roberson 2017년 10월 5일
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?

답변 (2개)

Niels
Niels 2017년 10월 5일
you could write the symbolic variable into file (ex. .txt file) -> open the file -> select all -> copy

Walter Roberson
Walter Roberson 2017년 10월 5일
편집: Walter Roberson 2017년 10월 5일
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.

Community Treasure Hunt

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

Start Hunting!

Translated by