REMOVE SPACING IN A STRING

721 ビュー (過去 30 日間)
Sadia
Sadia 2012 年 7 月 15 日
編集済み: Image Analyst 2021 年 7 月 6 日
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 件のコメント
Stephen23
Stephen23 2021 年 6 月 5 日
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 日
% 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 件のコメント
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"
Walter Roberson
Walter Roberson 2021 年 6 月 5 日
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 日
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 日
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 日
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by