0
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?

hhh
  • 50,788
  • 62
  • 179
  • 282

2 Answers2

3

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
Eitan T
  • 32,660
  • 14
  • 72
  • 109
  • 1
    Thank you, somehow skipped the part in manual -- luckily getting now more in-depth answers than even in manual :) – hhh Mar 28 '13 at 15:30
1

It is not straightforward to view the contents of a struct type in MATLAB. One quick approach is to do something like this:

r=struct2cell(solve(x==y+1, y^2==z,z==9));
r{:}

ans =

  4
 -2


ans =

  3
 -3


ans =

 9
 9

If you want to identify the actual variable names, I think you'll need to write a custom routine to print them how you'd like them to appear.

aardvarkk
  • 14,955
  • 7
  • 67
  • 96
  • Interesting, a way not mentioned in manuals -- perhaps becocing useful in the future but the other method looks simpler to use anyway +1 for good try. – hhh Mar 28 '13 at 15:29