Select parts of number from a cell of many numbers to put in a variable?

조회 수: 1 (최근 30일)
Ross
Ross 2014년 8월 12일
댓글: Ross 2014년 8월 13일
Hi.
I have used
[num text raw] = xlsread(filename)
to get access to the cells on a bastardised xml/csv type text file. I can read values from cells using
raw(row,col)
This is fine, however my question is, one of these cells contains the date in format
20140802
I wish to select parts of the number and assign variables for month(08), day(02), year(2014).
How exactly can I achieve this? I can't seem to find an answer to what seems to be an easy problem, help appreciated!

채택된 답변

Ben11
Ben11 2014년 8월 12일
편집: Ben11 2014년 8월 13일
The easy way would be:
year = YourDate(1:4)
month = YourDate(5:6)
day = YourDate(7:8)
Then you can convert to digits with str2double for example. Is that what you mean?
  댓글 수: 9
Ben11
Ben11 2014년 8월 13일
When you say "it seems to work but no title is present" do you use something like this:
TitleString = sprintf('Graph Plot at %d:%d on %d/%d/%d',hours,minutes,day,month,year);
... add code for your plot
title(TitleString)
if you don't see any title it might be because matlab does not put it at the right place?
Ross
Ross 2014년 8월 13일
I tried this (minus hours and minutes)
titleName = sprintf('Graph Plot at on %d/%d/%d',day,month,year)
title(titleName)
No error but the title is just blank. Also, I can't seem to add my 'time' variable with this syntax without it just printing time, whereas when I use:
titleName = (['Graph Plot at', time, 'on', Day, Month, year]);
title(titleName)
It does print the value of the variables except it's as though it's carriage returning after each comma. As mentioned, it is coming out like this:
Graph Plot at
time
on
06
08
2013
Wheras I would like it to display on the title as
Graph Plot at (time) on (day)(month)(year)
So close, it's probably a simple thing I'm missing or some title formatting option I haven't encountered.

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 8월 12일
a=20140802
b=datevec(datenum(num2str(a),'yyyymmdd'))
b(1) % is the year
b(2) % the month
b(3) % the day
  댓글 수: 3
Azzi Abdelmalek
Azzi Abdelmalek 2014년 8월 12일
Ross, If you run my code with my data, there are no errors. You just need to use the same class of data then mine, or use this one
a={20140802 20140803}
b=cellfun(@num2str,a,'un',0) % convert from numeric to string
c=datevec(cellfun(@(x) datenum(x,'yyyymmdd'),b))
Ross
Ross 2014년 8월 13일
편집: Ross 2014년 8월 13일
Hi, thanks for replying.
This code sorta works, I get the date parsed but it displays in column format, so my date cell displays like so:
2013
8
6
0
0
0
Original data, ie variable b looks like '20130806'. How to get it so I have variables such as:
Year = 2013
Month = 08
Day = 06
I don't quite understand this line in your code:
c=datevec(cellfun(@(x) datenum(x,'yyyymmdd'),b))
EDIT: Actually when I look at 'c' in the main window it displays as:
2013 8 6 0 0 0
This is for displaying in a title of a 2d plot. The title on the plot displays in columns and has the 3 unnecessary zero's which is why I was looking to separate the year, month and day to use easier with the title code which I have as:
titleName = (['Graph Plot at', time, 'on', day, month, year]);

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

카테고리

Help CenterFile Exchange에서 Title에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by