Converting string to number with spaces

I have a 3061622 long character, which is differentiated by three spaces, I want to form an array from this string such that a after each space a new number is formed. For example suppose I have the character such that, "111001000100 100000100000 1100100100" so the first element of the array formed has 111001000100, second has 100000100000 and so on.

2 件のコメント

Peng Li
Peng Li 2020 年 4 月 4 日
You could split the string by space and after that do a str2double. You might also want to change the specific type as well, as matlab by default stores all numerical numbers as double. You can use cast function to convert to different integer types.
Abdul Ghani Zahid
Abdul Ghani Zahid 2020 年 4 月 4 日
Li its a 3061622 long character, which is already split by spaces. These are binary values, which I want to extraxt in txt file. I tries str2double bit the command window displays "NaN".

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

 採用された回答

David Hill
David Hill 2020 年 4 月 4 日

0 投票

Because your string is so long, str2num will not work. Recommend to form a cell of arrays {[1 0 1 0 1 1],[1 0 1 1 1 1 1],...}.
a='10001001001 1000010101010111111 1001000100010010101001111';
b=cellfun(@(x)x-'0',regexp(a,'[0-9]*','match'),'UniformOutput',false);

7 件のコメント

Abdul Ghani Zahid
Abdul Ghani Zahid 2020 年 4 月 4 日
David it is converting but the comand window displays as follow
Columns 78597 through 78603
[1x32 double] [1x32 double] [1x32 double] [1x32 double] [1x32 double] [1x32 double] [1x32 double]
David Hill
David Hill 2020 年 4 月 4 日
編集済み: David Hill 2020 年 4 月 4 日
If the size will always be the same you could use a matrix instead.
b{1}%is the first array
b{2}%is the second array ...
If you just want the binary character arrays:
a='10001001001 1000010101010111111 1001000100010010101001111';
b=regexp(a,'[0-1]*','match');
And you could convert to decimal:
c=bin2dec(b);
Abdul Ghani Zahid
Abdul Ghani Zahid 2020 年 4 月 4 日
Thanks david that solved my problem, but now it is in cell form. I tried to use the following code to convert into txt file, it converted but each element started froma new line whereas I want each cell to start from the new line in the TXT file. How do I do that?
I used "dlmwrite('output.txt', ha, 'delimiter','\n','newline','pc','precision',1);" but each element in new line for example
1
1
1
1
0
0
and so on.
Abdul Ghani Zahid
Abdul Ghani Zahid 2020 年 4 月 4 日
where as I want,
1100100.........
1100100.........
and so on
David Hill
David Hill 2020 年 4 月 4 日
Are the lengths of the arrays all the same? Or do you care if you have padded zeros in front?
a='10001001001 1000010101010111111 1001000100010010101001111';
b=regexp(a,'[0-1]*','match');
c=bin2dec(b);
d=dec2bin(c,32);%whatever max length
Abdul Ghani Zahid
Abdul Ghani Zahid 2020 年 4 月 5 日
The length is the same, but I want the same numbers as these are fractions of the form(32,31). These are also signed so the MSB indicates the sign. So if there is a 0 in the beginning it indicates the sign of the number.
Abdul Ghani Zahid
Abdul Ghani Zahid 2020 年 4 月 5 日
Actually I have to get these in .txt file and use the data somewhere else

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

その他の回答 (2 件)

Ameer Hamza
Ameer Hamza 2020 年 4 月 4 日
編集済み: Ameer Hamza 2020 年 4 月 4 日

0 投票

try this
str = "111001000100 100000100000 1100100100";
num = cell2mat(textscan(str, '%u64'));
Result
num =
3×1 uint64 column vector
111001000100
100000100000
1100100100
If you want these binary numbers to be converted to decimal
str = "111001000100 100000100000 1100100100";
num = cell2mat(textscan(str, '%bu64'));
Result
num =
3×1 uint64 column vector
3652
2080
804

8 件のコメント

Abdul Ghani Zahid
Abdul Ghani Zahid 2020 年 4 月 4 日
Hamza the output isnt the same, maybe because each binary number is 32 bit.
Ameer Hamza
Ameer Hamza 2020 年 4 月 4 日
How is output different? Are you talking about the first code or the second one?
Abdul Ghani Zahid
Abdul Ghani Zahid 2020 年 4 月 4 日
I am talking about the first code, I want binary values. I guess the first code is to get the binary values but I get the values as follow
18446744073709551615
18446744073709551615
18446744073709551615
18446744073709551615
18446744073709551615
18446744073709551615
18446744073709551615
18446744073709551615
18446744073709551615
18446744073709551615
18446744073709551615
18446744073709551615
18446744073709551615
18446744073709551615
1110010010001010111
18446744073709551615
Ameer Hamza
Ameer Hamza 2020 年 4 月 4 日
Ok. I get it. Your binary numbers have 32 digits. MATLAB does not support a data type that can support 32 digits. In that case, your best bet is to save it in uint64 format. It is decimal value equivalent to the binary number. Try the second code.
Abdul Ghani Zahid
Abdul Ghani Zahid 2020 年 4 月 4 日
that doesnt work as well.
It gives the following error
b = cell2mat(textscan(SCG_SIG_BIN, '%bu64'));
Error using textscan
Unable to parse the format string at position 1 ==> %bu64
Unsupported format specifier '%b'. See the documentation for TEXTSCAN for supported formats.
Abdul Ghani Zahid
Abdul Ghani Zahid 2020 年 4 月 4 日
and second the whole purposse was to get the output in the the character in, with same zeros and ones as it is a 32 bit signed number, I converted the numbers using the fi function and then converted the fi array into binary using the bin function.
Ameer Hamza
Ameer Hamza 2020 年 4 月 4 日
I guess you are using some old version of MATLAB that is why textscan does not recognize '%bu64' format.
If you want to store the binary digits in a numeric form, then note that a decimal number, e.g., 101010, is not equivalent to the binary number (101010)b. It just contains 0 and 1 digits, but in reality, it is a number in base 10, you cannot apply any binary operations on it.
Walter Roberson
Walter Roberson 2020 年 4 月 4 日
%b is new as of R2019b or perhaps R2020a.

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

Walter Roberson
Walter Roberson 2020 年 4 月 4 日

0 投票

str = "111001000100 100000100000 1100100100";
cellfun(@bin2dec, regexp(str, '\s+', 'split'))

3 件のコメント

Walter Roberson
Walter Roberson 2020 年 4 月 4 日
Or as hinted by David Hill,
str = "111001000100 100000100000 1100100100";
bin2dec(regexp(str, '\s+', 'split'))
Abdul Ghani Zahid
Abdul Ghani Zahid 2020 年 4 月 5 日
The length is the same, but I want the same numbers as these are fractions of the form(32,31). These are also signed so the MSB indicates the sign. So if there is a 0 in the beginning it indicates the sign of the number.
Walter Roberson
Walter Roberson 2020 年 4 月 5 日
mask = c >= 2^31;
c(mask) = 2^31 - c(mask) ;
c = int32(c) ;
Note that this code does not bother to reconstruct -0 correctly. Binary integer encodings that have separate sign have the possibility of a number in which the sign bit is set but the other bits are 0, and that represents "negative zero". The difference between negative zero and positive zero is that positive number divided by 0 gives positive infinity but a positive number divided by negative zero gives negative infinity.

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

カテゴリ

ヘルプ センター および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by