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

20 ビュー (過去 30 日間)
MC
MC 2017 年 4 月 28 日
コメント済み: Walter Roberson 2020 年 12 月 1 日
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
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
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))
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
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
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
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
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 件のコメント
Stephen23
Stephen23 2017 年 4 月 28 日
編集済み: Stephen23 2020 年 11 月 24 日
Much simpler to use strrep:
>> str ='kadbehjwkbnwjkbfsjdbj\\\r\ndhjbfjsb';
>> strrep(str,'\\\r\n','')
ans =
kadbehjwkbnwjkbfsjdbjdhjbfjsb
John Chilleri
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
MC 2017 年 4 月 29 日
Thanks for the answers guys! Though these string operations doesn't seem to work on my symbolic expressions
  1 件のコメント
Walter Roberson
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!

Translated by