Undefined operator '-' for input arguments of type 'cell'.
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Star Strider
2020 年 9 月 15 日
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 件のコメント
Walter Roberson
2020 年 9 月 15 日
Note: years(2000) is 2000 fixed-length years. You should be adding calyears(2000) for calendar years.
その他の回答 (1 件)
Walter Roberson
2020 年 9 月 15 日
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 件のコメント
Walter Roberson
2020 年 9 月 15 日
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.
参考
カテゴリ
Help Center および File Exchange で Calendar についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!