Cell values to numbers
1 view (last 30 days)
Show older comments
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 Comment
Jan
on 24 Apr 2016
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.
Answers (1)
Image Analyst
on 24 Apr 2016
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 Comments
Image Analyst
on 24 Apr 2016
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).
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!