How to convert char to number?

조회 수: 15 (최근 30일)
Sandy
Sandy 2016년 6월 10일
댓글: Azzi Abdelmalek 2016년 6월 10일
I have a data set that I am trying to format as follows:
month = data (:, 2); % (2276x1)
month =sprintf('%02d\n',month)
The problem is this gives me:
Name Size Bytes Class
month 1x6828 13656 char
I need it to be a single column of NUMBER data (2276x1) in the format I specified. I tried using str2num but this makes my data lose the formatting I want.
Please help and thank you!
  댓글 수: 5
Azzi Abdelmalek
Azzi Abdelmalek 2016년 6월 10일
When you write:
a=05
This number will be displayed as 5. The only way to display it with padded 0 is to use a string.
sprintf('%02d',5)
Image Analyst
Image Analyst 2016년 6월 10일
Don't use month as a variable name because it's a built-in function. What does data represent, and especially column 2 of data? Why is month 2276 characters long? The month names I know of are less than 10 characters, like September.

댓글을 달려면 로그인하십시오.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 6월 10일
편집: Azzi Abdelmalek 2016년 6월 10일
B=sprintf('%02d\n',A)
sprintf is used to display your data A in a format of your choice, your data are converted to string B. Your data are still in your original array A. Why do you want to convert B ?
For example, if you have a number 5, if you want to display it as 05 you need to convert it to a string '05'.
  댓글 수: 4
Sandy
Sandy 2016년 6월 10일
Is there a way to save what is shown in the display? For example, if my numbers are 5, 6, 7, I'd like to export a text file with 05, 06, 07.
Azzi Abdelmalek
Azzi Abdelmalek 2016년 6월 10일
If you want to save your data as double, just save your original array. nobody will see it!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by