REMOVE SPACING IN A STRING

I want to convert my binary data into hex, the function that does so only that string as an input. but when I convert my 64 bit binary matrix into a string, it doesn't remove the spaces, which is messing my solution, any idea how to get rid of these
here is wat im talking about;
what i want: '0111001101100001011001000'
what i get: '0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 1 0 1 1 0 0 1 0 0 0'

4 comentarios

AYUSH VARSHNEY
AYUSH VARSHNEY el 31 de Mayo de 2021
And what about vice versa???
what i want : ''0 1 1 1 1 0 0 0 0 0 1 1 0 0 0" like this from "01110000011100"??
S = "01110000011100"
S = "01110000011100"
regexprep(S, '(.)(?=.)', '$1 ')
ans = "0 1 1 1 0 0 0 0 0 1 1 1 0 0"
S = "01110000011100";
sprintf(" %c",S{1})
ans = " 0 1 1 1 0 0 0 0 0 1 1 1 0 0"
Walter Roberson
Walter Roberson el 5 de Jun. de 2021
But then you have to get rid of the leading space ;-)

Iniciar sesión para comentar.

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 15 de Jul. de 2012
Editada: Image Analyst el 6 de Jul. de 2021

18 votos

% Add this code
A = '0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 1 0 1 1 0 0 1 0 0 0' % Has spaces
A = A(find(~isspace(A)))
You get a string with no spaces:
A =
'0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 1 0 1 1 0 0 1 0 0 0'
A =
'0111001101100001011001000'

9 comentarios

Sadia
Sadia el 15 de Jul. de 2012
thx alot
Jan
Jan el 15 de Jul. de 2012
The find() is not needed: A= A(~isspace(A)) is sufficient already.
pham quyet
pham quyet el 18 de Mzo. de 2015
thank you very much
AYUSH VARSHNEY
AYUSH VARSHNEY el 31 de Mayo de 2021
And what about vice versa???
what i want : ''0 1 1 1 1 0 0 0 0 0 1 1 0 0 0" like this from "01110000011100"??
Image Analyst
Image Analyst el 31 de Mayo de 2021
@AYUSH VARSHNEY, try this:
str = '0 1 1 1 1 0 0 0 0 0 1 1 0 0 0'
% Make it like this form "01110000011100"
str(str == ' ') = []
AYUSH VARSHNEY
AYUSH VARSHNEY el 4 de Jun. de 2021
i need to add spaces in this ..
Image Analyst
Image Analyst el 4 de Jun. de 2021
@AYUSH VARSHNEY, it looks like all the spaces are now gone so I'm glad my code worked for you.
AYUSH VARSHNEY
AYUSH VARSHNEY el 5 de Jun. de 2021
no no no... it won't work for me....the pic i showed to you is the string is without space...
which is like this = "000011101010001010011"
and what i want is the spaces between them .
like this = "0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0"
S = "01110000011100"
S = "01110000011100"
regexprep(S, '(.)(?=.)', '$1 ')
ans = "0 1 1 1 0 0 0 0 0 1 1 1 0 0"

Iniciar sesión para comentar.

Más respuestas (3)

jwiix
jwiix el 6 de Sept. de 2018
Editada: Image Analyst el 6 de Jul. de 2021

12 votos

As an alternative
A = '0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 1 0 1 1 0 0 1 0 0 0'
A = strrep(A,' ','') % Replace space with null.
It's slightly faster than the current logical indexing answer I think.
-------------------------------------------------------------------------------------------
K>> A= '0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 1 0 1 1 0 0 1 0 0 0'
A =
'0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 1 0 1 1 0 0 1 0 0 0'
K>> tic; A= A(~isspace(A)); toc
Elapsed time is 0.000653 seconds.
-------------------------------------------------------------------------------------------
K>> A= '0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 1 0 1 1 0 0 1 0 0 0'
A =
'0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 1 0 1 1 0 0 1 0 0 0'
K>> tic; A = strrep(A,' ',''); toc
Elapsed time is 0.000098 seconds.
:)

1 comentario

Shep Bryan
Shep Bryan el 6 de Jul. de 2021
This answer is much better than the accepted answer

Iniciar sesión para comentar.

Image Analyst
Image Analyst el 15 de Jul. de 2012
Editada: Image Analyst el 15 de Jul. de 2012

7 votos

Just set locations with spaces equal to null:
% Generate sample string.
theString = '0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 1 0 1 1 0 0 1 0 0 0'
% Now change existing string by setting locations with spaces equal to null.
theString(theString == ' ') = []
% Alternative method.
% Create a brand new string with a different name
% by extracting non-space elements.
stringWithoutSpaces = theString(theString ~= ' ')
Akansha Saxena
Akansha Saxena el 31 de Ag. de 2016

3 votos

requiredString = regexprep(theString, '\s+', '')

2 comentarios

Alexander Jensen
Alexander Jensen el 30 de Mzo. de 2018
Editada: Alexander Jensen el 30 de Mzo. de 2018
This also works on cell arrays containing strings! (at least as of version R2017a)
Example:
str = {'1 GC 2 H M', 'food nam nam';'hello world','meh bleb'};
requiredString = regexprep(str, '\s+', '')
requiredString =
2×2 cell array
'1GC2HM' 'foodnamnam'
'helloworld' 'mehbleb'
Thank you
Rajbir Singh
Rajbir Singh el 10 de En. de 2019
its works.
Thank you @Akansha Saxena

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Preguntada:

el 15 de Jul. de 2012

Editada:

el 6 de Jul. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by