Symbiological output using 'syms' and 'pretty' commands
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Share a link to this question
Hi all,
I am having issues outputting the factored polynomial using the below commands using MatLab 2021b (academic license) sybologically.
I have the Symbology Toolbox 'Installed' and can't determine what the issues is.
The online R2021b 'Run' command outputs (MatLab Desired Output.GIF red box) also aren't outputting in my Command Window on my license.
The 'Attachment with run function' also isn't working for the 2021b forum posting. When
I= imread('PolySolver.GIF')
Error using imread>get_full_filename (line 569)
File "PolySolver.GIF" does not exist.
File "PolySolver.GIF" does not exist.
Error in imread (line 371)
fullname = get_full_filename(filename);
is ommited (online) the answer (ans) is outputted as desired (MatLab Desired Output.GIF).
%% Factor Command
syms x
f1 = factor (x^3 + 2*x -3, x) % Directly below not showing on PC version of 2021b
% Make the factorisation 'pretty'
% Status: Not working
pretty(f1)
% Output
I= imread('PolySolver.GIF')
imshow = I % Attachment with run function not working for 2021b - Online posting
% Desired
(x - 1), (x^2 + x + 3)
Accepted Answer
Star Strider
on 25 Nov 2021
Experiment with the indexing into the expressions (since this is a straightforward solution to a number of otherwise difficult problems in MATLAB).
Are these the desired results?
%% Factor Command
syms x
f1 = factor (x^3 + 2*x -3, x) % Directly below not showing on PC version of 2021b
f1 = 
f1(1)
ans = 
f1(2)
ans = 
% Make the factorisation 'pretty'
% Status: Not working
pretty(f1)
( 2 )
x - 1, x + x + 3
% Output
I= imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/812799/PolySolver.GIF');
imshow(I) % Attachment with run function not working for 2021b - Online posting

Right-click to copy the full file name to open the file in the in the online Run application. (This works except for .m files, that are painful to use with it.)
.
11 Comments
Jay
on 26 Nov 2021
Hi Star Strider,
The ‘ans’ output are the desired results, yes.
Do I need to manually call the terms individually by specifying f1(1) and f1(2), or is there a way to automate all terms to output inside closed brackets once the factor routine has run?
Star Strider
on 26 Nov 2021
The calls to the elements can be put in a loop and completely automated in order to save them as a cell array —
%% Factor Command
syms x
f1 = factor (x^3 + 2*x -3, x) % Directly below not showing on PC version of 2021b
f1 = 
for k = 1:numel(f1)
term{k} = f1(k);
end
term
term = 1×2 cell array
{[x - 1]} {[x^2 + x + 3]}
f11_squared = expand(f1(1)^2) % Using Individual Factors In Subsequent Expressions
f11_squared = 
I am not certain that this gets us much farther, though.
Since ‘f1’ is an array, index into it to get the values as with any other array, so just use ‘f1(1)’ and ‘f1(2)’ as they are needed, as I did to create ‘f11_squared’. Creating individual names for them instead is strongly discouraged as poor programming practice.
.
Jay
on 28 Nov 2021
For some reason the 'f11_squared' command doesn't output in the way it should using the Symobology Toolbox.
Even when calling 'f11_squared' in the command window results in 'x^2 - 2*x + 1'.
I don't know why this happening.
Regarding obtaining the values from the dynamic array, does matlab allow for outputting all values in an array in the command window using the symoblogy toolbox syntax, or is that what you mean by 'I am not certain that this gets us much farther'?
Walter Roberson
on 28 Nov 2021
The nicely formatted version
only shows up for LiveScript .
Star Strider
on 28 Nov 2021
@Walter Roberson — Again, thank you!
@Jay — It should output to the Command Window with the code I’m using here. I’ve only used it here, not on my computer offlie, because it works satisfactorially here.
‘Regarding obtaining the values from the dynamic array, does matlab allow for outputting all values in an array in the command window using the symoblogy toolbox syntax, or is that what you mean by 'I am not certain that this gets us much farther'?’
I’m not certain what the desired result is, here. Putting them in a cell array simply does exactly that, not different from keeping them in the ordinary array they started with. It was an attempt to automate displaying all the terms.
The point is that, as the first example demonstrates:
syms x
f1 = factor (x^3 + 2*x -3, x) % Directly below not showing on PC version of 2021b
f1 = 
they will all show up in the Command Window at the same time by default if that is desired.
Displaying the elements of that array individually by subscripting them:
f1(1)
ans = 
f1(2)
ans = 
simply permits dealing with individual elements of the array if desired.
This is no different than with any other MATLAB array:
V = [pi 42]
V = 1×2
3.1416 42.0000
V(1)
ans = 3.1416
V(2)
ans = 42
Is this the desired explanation?
.
Jay
on 29 Nov 2021
@Walter Roberson Thanks Walter. I'll run code in Live Editor from now on for a more aesthetic algebraic output.
@Star Strider Thanks Star Strider. That does make sense.
Is there a way to get around expcitly calling (specifying the elements of) the cells of the array though?
Using pseudo-code:
for f1(1,:) % for all cells in the top row
% print / list cell values
end
or, is there a dynamic way to create variables within a specified boundary where I can assign their respective values for implicit output?
eg.
specify a variable: f1
specify boundary of suffix values: k=1:numel(f1)
specify concatination of variables: for f1 suffix: for 1:k new variable created (f1_a and f1_b)
assign the newly created dynamic variables their correalating cell values (f1_a = f1(1,1) and f1_b = f1(1,2))
output all dynamic variables created with f1 prefix to Command Window
or, have I confused you as to my desired output and way of thinking?
Walter Roberson
on 29 Nov 2021
Dynamic variable names are seldom a good idea.
Jay
on 29 Nov 2021
@Walter Roberson Your link shows all the potential problems I can run into using dynamic variable naming that I would never have perceived. Thanks again, Walter.
Using indexing then, how best can I output the values of an array's cells by giving it a maximum boundary so that said values are outputted into the Command Window?
Star Strider
on 29 Nov 2021
As Walter described, creating dynamic veriable names is rarely a good idea. (One generally supported use is to assign a matrix row or column to a single variable if it will be used a lot later in the code. This is just more convenient. However if dynamic variables are to be used in a loop or some such, they become very unwieldy very quickly, and create slow, inefficient code.) Note that I simply referred to ‘f1’ as matrix elements ‘f1(1)’ and ‘f1(2)’.
the relativelly new string variable type has some advantages that character and symbolic variables and arrays do not —
syms x
f1 = factor (x^3 + 2*x -3, x) % Directly below not showing on PC version of 2021b
f1 = 
f1s = string(f1)
f1s = 1×2 string array
"x - 1" "x + x^2 + 3"
f1s_plus = f1s(1) + " " + f1s(2)
f1s_plus = "x - 1 x + x^2 + 3"
‘have I confused you as to my desired output and way of thinking?’
To an extent, yes. Remember the Stones song ‘You Can’t Always Get What You Want’? That applies here. Some things are possible, and some just aren’t. Experiment with the string variable properties, since they likely come closest to what I believe is the desired result (although I’m still not certain what that is).
.
Jay
on 29 Nov 2021
Thanks @Star Strider, I'll look into the new string variable and if it doesn't do as I would like, I'll just have to get used to the output and deal with it :).
Thanks again for your help gents.
Star Strider
on 29 Nov 2021
As always (our) pleasure!
.
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)