Odd (\\\r\n) output from symbolic multiplication
조회 수: 6 (최근 30일)
이전 댓글 표시
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
Shekoofeh Abdollahi
2020년 12월 1일
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
2020년 12월 1일
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.
채택된 답변
Walter Roberson
2017년 4월 29일
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
Walter Roberson
2020년 11월 23일
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
2020년 11월 23일
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.
추가 답변 (2개)
John Chilleri
2017년 4월 28일
편집: John Chilleri
2017년 4월 28일
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
John Chilleri
2017년 4월 28일
편집: John Chilleri
2017년 4월 28일
Nice usage of string replace - I wasn't aware of strrep, but it is most definitely the better method. Thanks for the knowledge!
MC
2017년 4월 29일
댓글 수: 1
Walter Roberson
2017년 4월 29일
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.)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!