필터 지우기
필터 지우기

Cell values to numbers

조회 수: 3 (최근 30일)
Jay
Jay 2016년 4월 24일
댓글: Image Analyst 2016년 4월 24일
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
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
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
Jay
Jay 2016년 4월 24일
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 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 CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by