Cell values to numbers
1 回表示 (過去 30 日間)
古いコメントを表示
So I have cell (1 x 2) created using the strsplit command (DMS = strsplit(StrVal, '.')).
I then use the cell2mat command to convert the cell values to characters (M_Mat = cell2mat(DMS(1,2))).
I then use the str2num command to obtain numerical values (M_Num = Str2double(M_Mat)).
The test value in M_Mat is 0001.
The returned value in M_Num = 1
How do I get the M_Num to equal 0001?
The M_Num value is used to assess validity of a condition in a later if statement (thus be of the type number or double).
Do I need to put the decimal point back into to the value just after the strsplit command (thus specifying it as a decimal value) and if so how do I do that in a cell?
1 件のコメント
Jan
2016 年 4 月 24 日
I do not understand the problem. If you want '0001', simply omit the str2double .
It would be easier to understand the code, if you post the input data and the code in a form, which runs by copy&paste.
回答 (1 件)
Image Analyst
2016 年 4 月 24 日
If you want to have leading zeros in there, then you'll have to leave it as a string, because, of course, 00001 is the same as 1 if you're dealing with numbers. Or else leave it as a number less than 1 like you said.
2 件のコメント
Image Analyst
2016 年 4 月 24 日
To get the fractional part, subtract the integer part:
fractionalPart = abs(theNumber) - floor(theNumber);
If you need to keep the sign, then multiply by sign(theNumber).
参考
カテゴリ
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!