I have a number, such as 008123, that I want to split into 008 and 123 in Matlab. The dividing by 1000 with remainder and floor will not work here because of the leading zeros. I have a long vector of these numbers, but all will have 6 digits. Help!
Copyright © 2024 Q2A.ES - All rights reserved.
Answers & Comments
Verified answer
x = '008123';
x1 = x(1:3)
x2 = x(4:6)
A1 = 123456 A2 you want 12 A3 you want 34 A4 you want 56 If this was a text field, you could use RIGHT/MID/LEFT functions to split it out. But since they are numbers, you'll have to move the decimal point around and TRUNCate the number - meaning delete any digits after the decimal point. This will work for splitting any 6 digit number into 3, 2-digit cells A2=TRUNC(A1/10000) A3=TRUNC((A1-(A2*100))/100) A4=A1-(A2*10000)-(A3*100) ===================== Part two If you are dealing with a number that is of a variable number of digits, the text functions I mentioned are probably the best way to go. If you find that this approach (turning the numbers to text) creates problems in other calculations, you can use the approach I mentioned above, but with a more complex equation that looks at the number of digits in the input cell. Should note that if you start out with two digits for the year, excel will drop off any "leading zeros" if the cell is formatted as a number - meaning 0801029999999 becomes 801029999999. Obviously this would mess up your 2-digit year. If the cell is formatted as text, the leading zeros will be retained. Let's do text - A1=0801029999999 A2=MID(A1,1,2) A3=MID(A1,3,2) A4=MID(A1,5,2) In this case A2=08, A3=01 and A4=02
A possible solution is treating the numbers as strings. There are functions int2str and str2int to convert one to the other as needed. Cut apart strings with the function substr.