# Odd (\\\r\n) output from symbolic multiplication

17 views (last 30 days)
MC on 28 Apr 2017
Commented: Walter Roberson on 1 Dec 2020
Hello!
So i'm doing some symbolic multiplication and the resulting expression output is quite long. However it includes one part of odd symbolics, namely: \\\r\n mixed inbetween the rest of the syms variables. I understand this is a line changing command but how can I remove this from the output? It is important since I need to compare if this symbolic output is equal to another expression.
Shekoofeh Abdollahi on 1 Dec 2020
Hello! The post is for a long time ago, but I wanna ask if you could solve this problem? The answers in this page gave me an insight but I couldn't resolve the question. I want to do: solve(p==0, gamma) where p is a symbolic variable of gamma, and it contains\\\r\n due to some multiplication. How can I do it?
Walter Roberson on 1 Dec 2020
Symbolic expressions never contain \r or \n
char() of a Symbolic expressions might perhaps have contained \r or \n, but if p is char() of a symbolic expression, then p==0 would be comparing the character codes to binary 0, which is going to be false(size(p)) as the result. And my content fro 2017 says that char() of a symbolic expression did not contain \r or \n .
Where does the \r or \n come from, then? It comes from displaying the content of a symbolic expression to the command line. And then you want to copy and paste that into a MATLAB expression.
Don't Do That.
The display of symbolic expressions is in a form that is not MATLAB code and is not the internal symbolic language MuPAD either
syms t; char(piecewise(t < 3 & t > 1, 5, 9))
ans = 'piecewise(t in Dom::Interval(1, 3), 5, symtrue, 9)'
try evalc('piecewise(t in Dom::Interval(1, 3), 5, symtrue, 9)'); catch ME; disp(ME); end
MException with properties: identifier: 'MATLAB:m_missing_operator' message: 'Error: Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.' cause: {} stack: [7×1 struct] Correction: []
try evalin(symengine, 'piecewise(t in Dom::Interval(1, 3), 5, symtrue, 9)'); catch ME; disp(ME); end
MException with properties: identifier: 'symbolic:piecewise:CannotConvertToTypeBranch' message: 'Unable to convert to type 'branch'.' cause: {} stack: [10×1 struct] Correction: []
You can save() and load() symbolic expressions. You can use the symengine approach I posted in 2017 to produce MuPAD code that can be loaded back into MuPAD. You can use matlabFunction() to create a more-or-less equivalent numeric expression, But the displayed outputed output of a symbolic expression should not be copied and pasted into MATLAB code.

Walter Roberson on 29 Apr 2017
The extra characters used on OS-X and Linux are \n\n rather than \r\n .
The extra characters only affects default display. It does not affect char() of the symbolic expression.
Note that both the default display and char() of the symbolic expression run the risk of:
Output truncated. Text exceeds maximum line length for Command Window display.
You can get around this by stepping into MuPAD:
feval(symengine, '_assign', 'TemporaryName', NameOfYourSymbolicVariable);
evalin(symengine, 'write(Text, "NameOfTheOutputFile.txt", TemporaryName)')
then NameOfTheOutputFile.txt will contain the full text.
By the way: the output file will be written into whichever directory was your current directory at the time you did the first your first symbolic command in this session (or, if you have done "clear all", then the current directory at the time of your first symbolic command since then.) You can give an absolute path for certainty.
Walter Roberson on 23 Nov 2020
Don't Do That.
writing to file using those commands is for debugging purposes, or as a step in exporting for use with a different computer language that you already have a convertor for.
To solve the expression for zero, leave it in MATLAB and use vpasolve or solve, or use matlabFunction and a numeric zero finder.
Shekoofeh Abdollahi on 23 Nov 2020
I already used solve and vpasolve, and it found the answer. But when I substitute the answer in the variable (either by "subs" function or do it manually), the answer is not even close to zero. I don't know where's the problem, and that's why I thought it's because of symbolic variable. So, it may be irrelevant to this topic, but could you please help me with this problem.

John Chilleri on 28 Apr 2017
Edited: John Chilleri on 28 Apr 2017
Hello,
If it's just a one-timer, you can identify where it is and remove it by:
expr = [expr(1:before) expr(after:end)];
If this will be encountered multiple times, then I would create a function to search strings and remove it, for example:
function str = rm_garbage(string)
n = length(string);
for i = 6:n
if strcmp(string(i-5:i),'\\\r\n')
break;
end
end
str = [string(1:i-6) string(i+1:end)];
end
Running this with an arbitrary string produces:
>> string
string =
>> str
str =
where you can see the '\\\r\n' has been removed.
Hope this helps!
Stephen23 on 28 Apr 2017
Edited: Stephen23 on 24 Nov 2020
Much simpler to use strrep:
>> strrep(str,'\\\r\n','')
ans =
John Chilleri on 28 Apr 2017
Edited: John Chilleri on 28 Apr 2017
Nice usage of string replace - I wasn't aware of strrep, but it is most definitely the better method. Thanks for the knowledge!

MC on 29 Apr 2017
Thanks for the answers guys! Though these string operations doesn't seem to work on my symbolic expressions
Walter Roberson on 29 Apr 2017
symbolic expressions are not strings (or characters) unless you char() them, and if you do then you will not have the \r\n or \n\n in the result (but, as described above, it might be truncated.)

### Categories

Find more on Common Operations in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!