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

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 件のコメント

Ross
Ross 2014 年 8 月 12 日
This looks like it should work but as usual with my experience of matlab, it gives me errors before I even get to thinking about converting. Should I perhaps str2num my date variable first?
Ben11
Ben11 2014 年 8 月 13 日
Hum if you use str2num on your string you will get a double of size (1x1), and then accessing foe instance YourData(1:4) for the year will generate an error. What error message do you get when you run my code above?
Ross
Ross 2014 年 8 月 13 日
The error I get is:
Index exceeds matrix dimensions.
Error in myCode (line 54)
year = date(1:4);
Ben11
Ben11 2014 年 8 月 13 日
ok well this error would be expected if 20140802 were of class double, but not if it were a string. Are you sure it is indeed a string?
Ross
Ross 2014 年 8 月 13 日
Ah, thanks Ben! I got it now! I changed my date calling function from
raw(3,14);
to
num(3,14);
then converted to a string and your code worked great, thanks for that! :)
This is so I can use the variables to print out the date on a 2d plot title using:
titleName = (['Graph Plot at', time, 'on', Day, Month, year]);
Now the only problem I have is that the date is printed out like this
Graph Plot at time on
2013
08
06
Ben11
Ben11 2014 年 8 月 13 日
Great glad to help!
You can use sprintf to format your title as you like. Eg
title = sprintf('Graph Plot at %d:%d on %d/%d/%d',hours,minutes,day,month,year)
where hours and minutes are up to you of course.
Ross
Ross 2014 年 8 月 13 日
編集済み: Ross 2014 年 8 月 13 日
I can't seem to get this to work, I either get invalid format errors or it seems to work and no title is present. My version has a return carriage after every comma, whereas I would like it just as one line.
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 件のコメント

Ross
Ross 2014 年 8 月 12 日
This didn't work and gave me all sorts of errors unfortunately.
Undefined function 'fix' for input arguments of type 'cell'.
Error in num2str (line 66)
if ~isempty(x) && isequalwithequalnans(x, fix(x))
Error in filename (line 44)
b=datevec(datenum(num2str(date),'yyyymmdd'))
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]);

サインインしてコメントする。

カテゴリ

質問済み:

2014 年 8 月 12 日

コメント済み:

2014 年 8 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by