Converting string to number with spaces

조회 수: 15 (최근 30일)
Abdul Ghani Zahid
Abdul Ghani Zahid 2020년 4월 4일
댓글: Walter Roberson 2020년 4월 5일
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일
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월 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일
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
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일
str = "111001000100 100000100000 1100100100";
cellfun(@bin2dec, regexp(str, '\s+', 'split'))
  댓글 수: 3
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.

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by