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

元气小坏坏 提交于 2019-12-25 02:49:10

问题


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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/15685498/beautifying-the-output-of-matlab-aka-human-readable-form-for-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!