3

Currently matlab coder is not supporting strcat or strjoin. Is there any anyway to circumvent this or custom function?

Edit: Input= [a b c d] Expected output= 'a,b,c,d'

Ryan Livingston
  • 1,898
  • 12
  • 18
Joel
  • 1,650
  • 2
  • 22
  • 34
  • 1
    `[` and `]` for `strcat`? – Divakar Apr 11 '14 at 18:57
  • Probably relevant: [String functions supported by MATLAB Coder](http://www.mathworks.com/help/coder/ug/functions-supported-for-code-generation--categorical-list.html#bq1h2z8-31) – Ben Voigt Apr 11 '14 at 19:01
  • What is the usage of `strjoin`? Example command? – chappjc Apr 11 '14 at 19:06
  • @chappjc, strjoin also concatenate strings `str = strjoin(C,delimiter)` [matlab ref](http://www.mathworks.com/help/matlab/ref/strjoin.html) – Joel Apr 11 '14 at 19:14
  • @Joel I know the function, but I'm asking the specifics. What is the delimiter in your code? – chappjc Apr 11 '14 at 19:19
  • @chappjc I was using comma as delimiter. `str=strjoin(c,',')` – Joel Apr 11 '14 at 19:22
  • @Divakar, can you explain how we can use `[]` for `strcat`? Doesnt that just make an array? – Joel Apr 11 '14 at 19:28
  • @Joel Doesn't `strcat` make a bigger array too? For example, `strcat('a','b')` and `['a' 'b']` are equivalent. But I don't know how you are using strcat, so can't guarantee that it's replaceable everywhere. – Divakar Apr 11 '14 at 19:32
  • @Joel: There is no "string" in matlab. Everything is a row vector of characters. `[` and `]` should fix it in your case. – Daniel Apr 11 '14 at 19:34
  • 1
    @Daniel For `strcat` it depends on inputs. Consider `strcat({'Red','Yellow'},{'Green','Blue'})`, which gives `'RedGreen' 'YellowBlue'`. – chappjc Apr 11 '14 at 19:42

1 Answers1

4

For strjoin you might get away with sprintf:

>> colorCell = [{'Red','Yellow'},{'Green','Blue'}];
>> colorList = strjoin(colorCell,',')
colorList =
Red,Yellow,Green,Blue
>> colorList = sprintf('%s,',colorCell{:}); colorList(end)=[]
colorList =
Red,Yellow,Green,Blue

If you can't use spintf:

>> c = [colorCell(:) repmat({','},numel(colorCell),1)].';
>> colorList = [c{:}]; colorList(end)=[]

For strcat, simple usage is often equivalent to using [].

>> strcat(colorCell{:})
ans =
RedYellowGreenBlue
>> [colorCell{:}]
ans =
RedYellowGreenBlue

However, for more complex syntax, it's not that simple:

>> strcat({'Red','Yellow'},{'Green','Blue'})
ans = 
    'RedGreen'    'YellowBlue'

Do you need a solution for this usage? Perhaps the following:

colorCell1 = {'Red','Yellow'}; colorCell2 = {'Green','Blue'};
colorCell12 = [colorCell1;colorCell2];
c = mat2cell(colorCell12,size(colorCell12,1),ones(size(colorCell12,2),1));
cellfun(@(x)[x{:}],c,'uni',0)
chappjc
  • 30,359
  • 6
  • 75
  • 132
  • your solution allows to concatenate strings without using `strjoin` but unfortunately coder doesn't allow `sprintf` either. – Joel Apr 11 '14 at 20:20
  • Coder doesn't allow `cellfun` (at least as of 2015b which I can test), so that option is null too... – Wolfie Sep 01 '17 at 10:10