Is there a Matlab function to convert any data structure to a string?

前端 未结 4 1288
情话喂你
情话喂你 2020-12-10 16:26

I\'m looking for a function in Matlab to use for error messages, like so:

error([\'Invalid value for someVariable: \' wantedFunction(someVariable)]);
         


        
4条回答
  •  情书的邮戳
    2020-12-10 16:52

    Yes, although it's not straightforward. You have to use the disp in combination with evalc:

    string = evalc(['disp(someVariable)'])
    

    You could cast this into more manageable form:

    toString = @(var) evalc(['disp(var)']);
    

    So, for your example:

    >> var = {rand(3,1), 'A', struct('test', 5)};
    >> error(['Invalid value for var: ' toString(var)])
    
    ??? Invalid value for var:     [3x1 double]    'A'    [1x1 struct]
    

提交回复
热议问题