Kit
Kit

Reputation: 31513

How do I merge all the elements of a MATLAB array into a string?

If I have something like this:

m = [0 1 0 0 1 1]

I want to turn it into

s = '010011'

In Python, it's so easy:

m = [0, 1, 0, 0, 1, 1]
s = ''.join(m)
# s = '010011'

How do I do it in MATLAB?

Upvotes: 2

Views: 81

Answers (2)

Autonomous
Autonomous

Reputation: 9075

I think an alternate way could be this:

s=num2str(m);
s(s==' ')='';

or

s=regexprep(num2str(m),'[^\w]','')

Upvotes: 1

Floris
Floris

Reputation: 46375

Remember that Python does type conversions automatically - Matlab (and most other languages) is a little more picky. Thus, you will need to do the type conversion manually on every element of your array. I believe

myString = sprintf('%1d', m);

should do it - although I can't actually check it as I don't have matlab on my computer at home. Try it - tell me if that doesn't work for you.

Upvotes: 3

Related Questions