In Matlab, one can evaluate an arbitrary string as code using the eval
function. E.g.
s = '{1, 2, ''hello''}' % char
c = eval(s) % cell
Is there any way to do the inverse operation; getting the literal string representation of an arbitrary variable? That is, recover s
from c
?
Something like
s = repr(c)
Such a repr
function is built into Python, but I've not come across anything like it in Matlab, nor do I see a clear way of how to implement it myself.
The closest thing I know of is something like disp(c)
which prints out a representation of c
, but in a "readable" format as opposed to a literal code format.
functions()
function. – Rushyrepr(eval(s)) == s
to betrue
for anyeval
'able strings
representing a literal. – Stevenstevenamatrepr
. You wouldn't be able to pass an argument asmatrepr(cos)
ormatrepr(inv)
. You can for example dofunc2str(@cos)
– Rushycos
is not a literal. If I can get it to work for the example in the question, the solution is general enough. – Stevenstevenarepr(c)
would be very useful. – Stevenstevena