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

17 views (last 30 days)

Show older comments

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.

##### 2 Comments

Shekoofeh Abdollahi
on 1 Dec 2020

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))

try evalc('piecewise(t in Dom::Interval(1, 3), 5, symtrue, 9)'); catch ME; disp(ME); end

try evalin(symengine, 'piecewise(t in Dom::Interval(1, 3), 5, symtrue, 9)'); catch ME; disp(ME); end

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.

### Accepted Answer

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.

##### 6 Comments

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

Thank you for your helpful comments. I was so lucky to find this page after getting disappointed to solve my problem!

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.

### More Answers (2)

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 =

kadbehjwkbnwjkbfsjdbj\\\r\ndhjbfjsb

>> str

str =

kadbehjwkbnwjkbfsjdbjdhjbfjsb

where you can see the '\\\r\n' has been removed.

Hope this helps!

##### 2 Comments

John Chilleri
on 28 Apr 2017

Edited: John Chilleri
on 28 Apr 2017

MC
on 29 Apr 2017

##### 1 Comment

Walter Roberson
on 29 Apr 2017

### See Also

### Categories

### Products

### Community Treasure Hunt

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

Start Hunting!