convert class 'char' to class 'logical' , How?

I have some binary bits in a string of class 'char':
b= 10001010101010101000000111110000111
I wish to covert class of 'b' from 'char' to 'logical'.
When i use str2num function, it considers all the bits as a single number and returns me
str2num(b)
ans =
1.0001e+034
Which i don't want. i want b as a logical array.
Kindly Help.

 Respuesta aceptada

Mischa Kim
Mischa Kim el 17 de En. de 2014

9 votos

Try
b_bin = logical(b(:)'-'0')

7 comentarios

Furqan Haider
Furqan Haider el 17 de En. de 2014
Thank you, that is exactly what i wanted.
Michael Pugh
Michael Pugh el 24 de Jun. de 2020
This works but I can't really understand it. Clever though.
@Michael Pugh Another solution that works is
b_bin = logical(b'-'0')
The -'0' part tells Matlab to subtract the ASCII value of 0, namely 48, from the ASCII values of b', which will be 48 or 49 depending on whether the entry is 0 or 1. In the end, b'-'0' will be a vector of doubles, and we convert that to a logical vector. (See also https://stackoverflow.com/questions/20032413/matlab-string-vector-character-subtraction .)
b= '10001010101010101000000111110000111'
b_bin = logical(b(:)'-'0')
How to convert back the b_bin to b
b = char(b_bin+'0')
Thushara
Thushara el 6 de Mzo. de 2024
Editada: Thushara el 6 de Mzo. de 2024
"b_bin = logical(b(:)'-'0')"
Why this is not indicated in the Documentation for 'logical' function?
Stephen23
Stephen23 el 6 de Mzo. de 2024
Editada: Stephen23 el 6 de Mzo. de 2024
"Why this is not indicated in the Documentation for 'logical' function?"
It already is: it states at the very top "logical(A) converts A into an array of logical values. Any nonzero element of A is converted to logical 1 (true) and zeros are converted to logical 0 (false)", which is exactly what happens here.
Of course the LOGICAL documentation cannot cover every possible way that you might create the input array (you did not consider that there are many many many many other ways of creating the input array and there is nothing special about this particular way so your proposal would mean including basically a very large subset of the entire MATLAB documentation on every single MATLAB documentation page... such documentation would be truly very large and very difficult to find anything in).

Iniciar sesión para comentar.

Más respuestas (2)

William Prophet
William Prophet el 11 de Nov. de 2016
If you want to convert the character array to a logical one, just compare the entire array character by character. So if you have '001011' and you want to return [0 0 1 0 1 1], just do
'001011' == '1'
And then every character in the array is compared to '1'.
Andreas Goser
Andreas Goser el 17 de En. de 2014

1 voto

Logical can only hold 0 or 1, so 10001010101010101000000111110000111 is not possible. Maybe you want binary numbers?

2 comentarios

Furqan Haider
Furqan Haider el 17 de En. de 2014
yes, you are right, binary holds 0 and 1, but i have a variable 'b' of class 'char' , i want to separate its elements and put them in a logical or 'double' class array.
How can I get it,
Kindly help
Furqan Haider
Furqan Haider el 17 de En. de 2014
as i have already stated that class of variable 'b' is 'char',
b= ['10001010101010101000000111110000111']

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Preguntada:

el 17 de En. de 2014

Editada:

el 6 de Mzo. de 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by