Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab Coder - Alternative for strcat function

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'

like image 218
Joel Avatar asked Feb 06 '26 14:02

Joel


1 Answers

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)
like image 111
chappjc Avatar answered Feb 09 '26 12:02

chappjc