Search code examples
matlabmathoutputsymbolic-math

Beautifying the output of Matlab aka human-readable form for output?


syms x y z;
solve(x==y+1, y^2==z,z==9) 

ans = 

x: [2x1 sym] 
y: [2x1 sym] 
z: [2x1 sym]

and now I want to see the results like Mathematica outputting {{x->-2,y->-3,z->9},{x->4,y->3,z->9}} for Solve[{x == y + 1, y^2 == z, z == 9}, {x, y, z}]. The workspace window and then variable editor shows me this but I still cannot see the real values stored there.

How can I see the output of Matlab in human-readable form aka beautified form?


Solution

  • The documentation of solve states:

    When solving a system of equations, use one output argument to return the solutions in the form of a structure array

    The result is returned as a struct, so you can access each field to see its value. The documentation brings an example of how to do it:

    S = solve(x==y+1, y^2==z, z==9);
    [S.x, S.y, S.z]
    

    This should result in:

    ans =
         4     3     9
        -2    -3     9
    

    Alternatively, you can return the solutions in separate variables by specifying multiple output arguments:

    [solx, soly, solz] = solve(x==y+1, y^2==z, z==9)
    

    and this will result in:

    solx =
         4
        -2
    
    soly =
         3
        -3
    
    solz =
         9
        -9