不支持Matlab编码器num2str [英] Matlab coder num2str not supported

查看:437
本文介绍了不支持Matlab编码器num2str的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过使用Matlab编码器将Matlab项目转换为C ++.我的代码中没有几个地方可以使用num2str函数.但是,当尝试使用Matlab编码器构建项目时,出现以下错误.

I am trying to convert a Matlab project into C++ by using Matlab coder. I have few places in my code that I use num2str function. But when trying to build the project using Matlab coder I get the following error.

独立代码生成不支持功能'num2str'."

"The function 'num2str' is not supported for standalone code generation."

在需要为结构创建字段标识符的情况下,使用了此功能.

I used this function in cases where I needed to create a field identifier for structs.

例如:

for i=1:numel(bvec)
      fId = ['L', num2str(i)];
      tmp = mystruct.(fId);  
      % do some work here  
end

对于功能num2str,我是否可以进行项目转换?

Is there an alternative to the function num2str for me to be able to convert the project?

推荐答案

使用sprintf会很容易,但是我不确定是否可以使用它?

Using sprintf would be easy but I'm not sure if you can use it?

fId = sprintf('L%d', i);

如果numel(bvec)的范围是0到9,则可以使用char:

If numel(bvec) is in the range 0 to 9 you could use char:

fId = ['L', char(48+i)];

或者您可以创建自己的数字到字符串的转换功能.也许有更好的方法,但这是一个主意:

Or you could create your own number to string conversion function. There may be better ways, but here's an idea:

function s = convertnum(n)
   if n > 9
      s = [convertnum(floor(n/10)), char(48+mod(n,10))];
   else
      s = char(48+n);
   end
end

然后像这样使用它:

fId = ['L', convertnum(i)];

编辑

基于注释的另一种转换功能:

An alternative conversion function based on comments:

function s = convertnum(n)
   s = [];
   while n > 0
      d = mod(n,10);
      s = [char(48+d), s];
      n = (n-d)/10;
   end
end

这篇关于不支持Matlab编码器num2str的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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