MATLAB Answers

Undefined operator '-' for input arguments of type 'cell'.

3 views (last 30 days)
Vijay
Vijay on 14 Sep 2020
Commented: Vijay on 17 Sep 2020
Hello,
I am trying to subtract two columns (that are in time domain) in spread sheet and create a new column. I attached the .xlsx file for your reference.
T = readtable('T.xlsx');
T.Time=T.BIRTHDT-T.INFODT;
But I am getting an error message: Undefined operator '-' for input arguments of type 'cell'.
I presume either the format of the two columbs might be different or one column is missing the hours/min/seconds information. But not sure how to sort.
Any help would be highly appreciated.

  0 Comments

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 15 Sep 2020
Try this:
T = readtable('FindDifference.xlsx');
T.INFODT = datetime(T.INFODT);
T.BIRTHDT = datetime(T.BIRTHDT);
T.Time = years(T.BIRTHDT-T.INFODT);
This puts ‘T.Time’ in units of years. Other units are possible. See the documentation for more information.

  10 Comments

Show 7 older comments
Star Strider
Star Strider on 15 Sep 2020
As always, my pleasure!
The plotting is not weird if it does what you want it to.
Thank you!
Walter Roberson
Walter Roberson on 15 Sep 2020
Note: years(2000) is 2000 fixed-length years. You should be adding calyears(2000) for calendar years.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 15 Sep 2020
filename = 'FindDifference.xlsx';
opt = detectImportOptions(filename);
opt = setvaropts(opt, {'BIRTHDT', 'INFODT'}, 'Type', 'datetime');
T.Time=T.BIRTHDT-T.INFODT;
T.Format = 'y';
However, I think you will be rather startled at the results.
The INFODT column has hard-coded into it years such as 0011 . This is not just a mistake of interpretation of numeric values: the column is a text column, not a date column or a numeric column (I checked in Excel.) If someone deliberately wanted to code in year 11 CE then that is probably how they would code it.
I would suggest that probably before doing the subtraction, you should have
T.INFODT = T.INFODT + calyears(1900);

  3 Comments

Vijay
Vijay on 15 Sep 2020
Hi Walter,
Thank you so much for your time.
Regarding the INFODT, such format is just made by matlab read an excel sheet using readtable function. The recoded dates are in years beyond 2000. I am attaching two individual date files joined together as shown below.
T1 = readtable('INFODT.xlsx') ;
T2 = readtable('BIRTHDT.xlsx');
T = join(T1,T2);
writetable(T,'T.xlsx')
I am computing the difference (i.e. T.Time = T.INFODT - T.BIRTHDT ;) to plot NHY vs the age (i.e. T.Time). But the format issues are not allowing to compute the age.
Coming to your suggested code, it's some how not working to me. Do you think there is an alternative in this case?
Walter Roberson
Walter Roberson on 15 Sep 2020
Which MATLAB release are you using? When I try in R2020a (on Mac), the dates associated with INFODT did not get written in the format of the original file you posted. Instead, the INFODT column in T got written as an Excel date with custom format . The original file you posted has, for example, 01-Feb-0011 00:00:00 as a pure text column.
Vijay
Vijay on 15 Sep 2020
I have been using 2019b (Windows) which resulted in the format below.
3000 '01-Apr-0016 00:00:00' 0 '01-Jan-1959'
3000 '01-Feb-0011 00:00:00' 0 '01-Jan-1945'
3000 '01-Feb-0013 00:00:00' 0 '01-Jan-1948'
3000 '01-Feb-0018 00:00:00' 0 '01-Jan-1954'
Now, when I tried with 2020a the format became...
3000 'Feb-0011' 0 01-Jan-1941
3000 'Mar-0012' 0 01-Jan-1941
3000 'Feb-0013' 0 01-Jan-1941
3000 'Mar-0014' 0 01-Jan-1941
Not sure why the year is taken as 0011 instead of 2011.
In any case, still I can't subtract the column values.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!