简体   繁体   中英

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?

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.

I think an alternate way could be this:

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

or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM