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 个评论

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.
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".

请先登录,再进行评论。

 采纳的回答

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 个评论

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]
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);
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.
where as I want,
1100100.........
1100100.........
and so on
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
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.
Actually I have to get these in .txt file and use the data somewhere else

请先登录,再进行评论。

更多回答(2 个)

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 个评论

Hamza the output isnt the same, maybe because each binary number is 32 bit.
How is output different? Are you talking about the first code or the second one?
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
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.
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.
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.
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.
%b is new as of R2019b or perhaps R2020a.

请先登录,再进行评论。

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

3 个评论

Or as hinted by David Hill,
str = "111001000100 100000100000 1100100100";
bin2dec(regexp(str, '\s+', 'split'))
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.
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 Exchange 中查找有关 Numeric Types 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by