Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine several string into one string in matlab

Tags:

matlab

I have variable A containing several string array as follows :

'0'    '->'    '2'      '1.000000'    '1.000200'    'A-MPDU'     '1.000000'
'0'    'NO'    'NaN'    '1.000270'    '1.000570'    'BACKOFF'    'NaN'     

I want to make those strings into one string form like this :

'0 -> 2 1.000000 1.000200 A-MPDU 1.000000'
'0 NO NaN 1.000270 1.000570 BACKOFF NaN'   

How to realize this using matlab?

like image 594
bnbfreak Avatar asked Dec 25 '22 05:12

bnbfreak


2 Answers

Presumably A is a cell array, so you could convert one row of it to an array of characters with

char(cellfun(@(x)[x ' ']',C(1,:),'UniformOutput',false))'

Note that we use cellfun to apply our anonymous function to each character array (element) of the cell array. The function appends a space to the character array (i.e. '0' becomes '0 '), and then we transpose this result to get a column. We do this for each element so that the final result is a column of characters which we do one final transpose on to get the string.

For example

>> char(cellfun(@(x)[x ' ']',C(1,:),'UniformOutput',false))'

   ans =
         0 -> 2 1.000000 1.000200 A-MPDU 1.000000 

>> char(cellfun(@(x)[x ' ']',C(2,:),'UniformOutput',false))'

   ans =
         0 NO NaN 1.000270 1.000570 BACKOFF NaN 

Try the above and see what happens!

like image 62
Geoff Avatar answered Jan 07 '23 14:01

Geoff


Method 1:

If you want to combine two strings together,use strcat

Example:

str = strcat('Good', 'morning')

str =

Goodmorning

But you need spaces in between strings:

So you have to change your strings to something like: '0 ' '-> ' '2 ' to get the string that you want.

Another method:

a='aaaa';
b='bb';
c=sprintf('%s %s',a,b); 
like image 33
lakshmen Avatar answered Jan 07 '23 16:01

lakshmen