2

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.

"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.

Eg:

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

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

4
  • @excaza thats not supported, List of supported functions, fprintf is supported, with lots of contraints... Commented Aug 27, 2014 at 12:36
  • Well, the constraints are not that unexpected. The function behaves more like c than matlab, so in most cases a c call should do the trick. Commented Aug 27, 2014 at 13:04
  • 1
    Would you be able to convert it to its corresponding unicode values you think? The you would be able to cast it. It is a bit of a detour, but it should at least work. The problem is not very hard though, you can have a list of unicode characters in a vector. Set the corrensponding values in the right places so that a call uniVec(3) will return the unicode value for 3 for example. Also you will have to handle the case of numbers with more than 1 digit in some way. Commented Aug 27, 2014 at 13:19
  • 1
    You have a bigger problem, which is that dynamic field names aren't supported by MATLAB Coder either. Commented Aug 27, 2014 at 14:34

3 Answers 3

4

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

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

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

Then use it like this:

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

EDIT

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
Sign up to request clarification or add additional context in comments.

5 Comments

sprinf is not supported. Otherwise, nice solution to convert the numbers to ascii. However, it think that a for loop would be sufficient. This is easy to loop and a would not be hard to read. That would really be the only reasons to use recursion since it is a known problem that recursion is inefficient in terms of calculation time.
Thanks @patrik I've added a looped version as well.
@sclarke81 Yes. It's working. This is the best solution for me at the moment. Thanks
@sclarke81 Oops.. Now I get a new problem in code generation readiness issues. "Recursive functions like convertnum.m are currently unsupported for code generation."
This may very well be the only solution. And it is really no magic to it. This is the most fundamental way to create a char and is based on the way a char is stored in memory.
2

I wrote the following code for Matlab2016a Coder to replace num2str, it also supports double precision:

function str = DoubleArray2String(x)
    str_cell=cell(1,length(x));
    for i=1:length(x)
        n = x(i);
        l = fix(n);
        r = n-l;
        str_cell{i} = strjoin({Double2String(l),Reminder2String(r)},'.');
    end
    str = strjoin(str_cell,',');
end

function str = Double2String(n)
    str = '';
    while n > 0
        d = mod(n,10);
        str = [char(48+d), str];
        n = (n-d)/10;
    end
    if isempty(str)
        str='0' ;
    end
end

function str = Reminder2String(n)
    str = '';
    while (n > 0) && (n < 1)
        n = n*10;
        d = fix(n);
        str = [str char(48+d)];
        n = n-d;
    end
    if isempty(str)
        str='0' ;
    end
end

Comments

0

A function equivalent of Matlab's num2str can be written using to_string in C++. Please see my version of the function:

#include"stdafx.h"
#include <sstream>
#include <string.h>

using namespace std;

string num2str(int number)
{
    string s;
    s = to_string(number);
    return s;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.