问题描述
我正在尝试通过使用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.
在需要为结构创建字段标识符的情况下,使用了此功能.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!