Odd (\\\r\n) output from symbolic multiplication
Ältere Kommentare anzeigen
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 Kommentare
Shekoofeh Abdollahi
am 1 Dez. 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?
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.
Akzeptierte Antwort
Weitere Antworten (2)
John Chilleri
am 28 Apr. 2017
Bearbeitet: John Chilleri
am 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 Kommentare
>> str ='kadbehjwkbnwjkbfsjdbj\\\r\ndhjbfjsb';
>> strrep(str,'\\\r\n','')
ans =
kadbehjwkbnwjkbfsjdbjdhjbfjsb
John Chilleri
am 28 Apr. 2017
Bearbeitet: John Chilleri
am 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
am 29 Apr. 2017
0 Stimmen
1 Kommentar
Walter Roberson
am 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.)
Kategorien
Mehr zu Common Operations finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!