Effacer les filtres
Effacer les filtres

Cell values to numbers

3 vues (au cours des 30 derniers jours)
Jay
Jay le 24 Avr 2016
Commenté : Image Analyst le 24 Avr 2016
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 commentaire
Jan
Jan le 24 Avr 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.

Connectez-vous pour commenter.

Réponses (1)

Image Analyst
Image Analyst le 24 Avr 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 commentaires
Jay
Jay le 24 Avr 2016
Ok, I would like to use a cell value that has an integer number and input the decimal point preceding said value in a created cell.
How do I do that?
Image Analyst
Image Analyst le 24 Avr 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).

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by