MATLAB Answers

REMOVE SPACING IN A STRING

1.426 views (last 30 days)
Sadia
Sadia on 15 Jul 2012
Commented: Rajbir Singh on 10 Jan 2019
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'

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 15 Jul 2012
Edited: Azzi Abdelmalek on 15 Jul 2012
%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'
A= A(find(~isspace(A))),
  3 Comments
pham quyet
pham quyet on 18 Mar 2015
thank you very much

Sign in to comment.

More Answers (3)

Image Analyst
Image Analyst on 15 Jul 2012
Edited: Image Analyst on 15 Jul 2012
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 ~= ' ')

jwiix
jwiix on 6 Sep 2018
As an alternative
A = strrep(A,' ','')
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.
:)

Akansha Saxena
Akansha Saxena on 31 Aug 2016
requiredString = regexprep(theString, '\s+', '')
  2 Comments
Rajbir Singh
Rajbir Singh on 10 Jan 2019
its works.
Thank you @Akansha Saxena

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by