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

AYUSH VARSHNEY
AYUSH VARSHNEY 2021년 5월 31일
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 2021년 6월 5일
But then you have to get rid of the leading space ;-)

댓글을 달려면 로그인하십시오.

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2012년 7월 15일
편집: Image Analyst 2021년 7월 6일

18 개 추천

% 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

Sadia
Sadia 2012년 7월 15일
thx alot
Jan
Jan 2012년 7월 15일
The find() is not needed: A= A(~isspace(A)) is sufficient already.
pham quyet
pham quyet 2015년 3월 18일
thank you very much
AYUSH VARSHNEY
AYUSH VARSHNEY 2021년 5월 31일
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"??
@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 2021년 6월 4일
i need to add spaces in this ..
Image Analyst
Image Analyst 2021년 6월 4일
@AYUSH VARSHNEY, it looks like all the spaces are now gone so I'm glad my code worked for you.
AYUSH VARSHNEY
AYUSH VARSHNEY 2021년 6월 5일
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"

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

jwiix
jwiix 2018년 9월 6일
편집: Image Analyst 2021년 7월 6일

12 개 추천

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

Shep Bryan
Shep Bryan 2021년 7월 6일
This answer is much better than the accepted answer

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2012년 7월 15일
편집: Image Analyst 2012년 7월 15일

7 개 추천

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 2016년 8월 31일

3 개 추천

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

댓글 수: 2

Alexander Jensen
Alexander Jensen 2018년 3월 30일
편집: Alexander Jensen 2018년 3월 30일
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 2019년 1월 10일
its works.
Thank you @Akansha Saxena

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

질문:

2012년 7월 15일

편집:

2021년 7월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by