made input into string but it's not showing all the numbers I need

1 回表示 (過去 30 日間)
eel
eel 2018 年 1 月 16 日
回答済み: Darshan Ramakant Bhat 2018 年 1 月 18 日
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 件のコメント
eel
eel 2018 年 1 月 16 日
sorry, the comment after str3 should say last 9 numbers, not 10

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

回答 (1 件)

Darshan Ramakant Bhat
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.

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by