When I type:
>> syms x y
>> solve(y-x^2,x)
I get:
ans =
[ 1/2 ]
[y ]
[ ]
[ 1/2]
[-y ]
How do I change the output format so to get MATLAB code (with * and ^), which by the way is what I used to get (before today)?

2 Kommentare

@Peter van der Kamp use texlabel function
syms x y
sol = solve(y-x^2,x);
sol
sol = 
solStr = texlabel(sol)
solStr = '[-{y}^{{1}/{2}}; {y}^{{1}/{2}}]'
Thanks, it's nice to know that I can turn things into tex. But it's not what I want. I'd like to define the solutions as branches of a multivalued function which I can feed back into MATLAB. Of course, this is just a simple example which I created for the issue I'm having. I used to get my answer in the following format: e.g. sqrt(5/2 + sqrt(w^2 + 9/4)) which is what I'm after. I'm using version R2022b.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Change the symbolic preference of Type-setting as follows -
syms x y
sol = solve(y-x^2,x)
sol = 
%Preference changed (Default value is true)
sympref('TypesetOutput',false);
%Check the output now
sol
sol = -y^(1/2) y^(1/2)

8 Kommentare

MATLAB returns me the following error:
Unrecognized function or variable 'sympref'.
Do you have the Symbolic Math Toolbox licensed and installed?
Also, which version of MATLAB are you using?
I'm using MATLAB R2022b, all was working fine untill today. Something has definitely changed! It has not complained about license issues. Can you tell me how to check that the Symbolic Toolbox is still licensed and installed? Thanks for your help
Enter the command "ver" in the command window and see if it is installed.
However, if you can run the code you have shown, you should have it installed.
Run these commands
restoredefaultpath;
rehash toolboxcache
and after that check (and share) the output of this particular command -
which sympref -all
/MATLAB/toolbox/symbolic/symbolic/sympref.m
Typing ver gave me a big list, which did not contain "Symbolic Math Toolbox". I'll spare you the list, but have it on file for the IT-department. Then I typed what you said and it gave me:
C:\Program Files\MATLAB\R2022b\toolbox\symbolic\symbolic\sympref.m
Then I typed ver again and I got, somewhere in the list:
Symbolic Math Toolbox Version 9.2 (R2022b)
Hurray!
However, when I now type syms x y I get:
The class 'sym' is used in its own class definition as a base class, property value, or attribute
value. This is not allowed.
Error in syms (line 231)
defined = sym(zeros(1, length(args)));
When closing MATLAB and reopening I'm back to where I was, namely
syms x y
>> solve(x-y^2,y)
ans =
[ 1/2 ]
[x ]
[ ]
[ 1/2]
[-x ]
and ver giving me a list which does not include the Symbolic Math Toolbox. Current plan is to ask IT to reinstall MATLAB and hope this solves the problem. Thanks again!
Based on your comments, things seem to be mixed up.
Yes, reinstalling MATLAB should clear the issue.
Let us know what happens after reinstalling.
Hi Dyuman, good news here:
>> syms x y
>> solve(x-y^2,y)
ans =
-x^(1/2)
x^(1/2)
After reinstalling MATLAB and restarting my computer, things are working well again. IT suggested that maybe a restart of the computer could have fixed the problem (I tend to not do that often enough, sorry).
Cheers,
Peter

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Ninad
Ninad am 28 Mai 2024
Hi Peter,
If you're using MATLAB version R2016b or newer, you can use "string(sol)" to convert the solution to a string format.
For versions older than R2016b, please use "char(sol)" to achieve the same outcome.
Below is a code snippet for your reference:
>> syms x y
>> sol = solve(y-x^2,x);
>> solStr = string(sol);
>> solChar = string(sol);
>> disp(solStr)
"-y^(1/2)"
"y^(1/2)"
>> disp(solChar)
"-y^(1/2)"
"y^(1/2)"
I hope you find this information helpful.
Regards,
Ninad

1 Kommentar

I'm on R2022b, when I type string(sol) I get:
Error using string
Conversion to string from sym is not possible.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Platform and License finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by