是否有Matlab函数将任何数据结构转换为字符串? [英] Is there a Matlab function to convert any data structure to a string?

查看:100
本文介绍了是否有Matlab函数将任何数据结构转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找Matlab中用于产生错误消息的函数,如下所示:

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

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

我希望wantedFunction能够同时接受字符串,数组,单元格数组,理想情况下甚至可以接受结构数组.

I want wantedFunction to be able to take both strings, arrays, cell arrays, ideally even structure arrays.

目前,我正在使用num2str,但这不适用于单元格.我以为我想用sprintf可以完成什么,但是我还没有弄清楚该怎么做.我想我可以编写自己的函数,但是如果在Matlab中已经可以做到这一点,那将是多余的.有什么想法吗?

For now, I'm using num2str, but that doesn't work for cells. I thought what I want could be done with sprintf, but I haven't figured out how. I suppose I could write my own function, but that would be redundant if there already is a way to do this in Matlab. Any ideas?

推荐答案

是的,尽管不是很简单.您必须将dispevalc结合使用:

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]

这篇关于是否有Matlab函数将任何数据结构转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆