made input into string but it's not showing all the numbers I need
11 次查看(过去 30 天)
显示 更早的评论
I have to extract certain numbers from an input. I turned it into a string to use the extractAfter and extractBefore functions. This is what the code looks like:
inputID = input('Please input an ID:');
createDate = input('Please input the creation date in MMDDYYYY format:');
str1 = string(inputID)
%str1 is equal to the numbers of the inputted ID
str2 = extractAfter(str1,5)
%str2 is the ID after the first 5 numbers have been removed
str3 = extractBefore(str2,9)
%str3 is the str2 after the last 10 numbers have been removed
%str3 results in the part of ID that is the supposed creation date
While the extracting is working as it should, the string function turns my input into "1.08592017101e+18." When I extract the parts, it doesn't include the other numbers after the "e," which I need. Is there a way to make it into a string without it becoming "1.08592017101e+18" ?
回答(1 个)
Darshan Ramakant Bhat
2018-1-18
There can be multiple ways to resolve this. One simple way is to using sprintf() to specify the format. The code will be like below:
inputID = input('Please input an ID:');
createDate = input('Please input the creation date in MMDDYYYY format:');
str1 = sprintf('%18d',inputID);
%str1 is equal to the numbers of the inputted ID
str2 = extractAfter(str1,5)
%str2 is the ID after the first 5 numbers have been removed
str3 = extractBefore(str2,9)
%str3 is the str2 after the last 9 numbers have been removed
%str3 results in the part of ID that is the supposed creation date
I hope this is what you wanted.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!