How to convert class double to class table ?

14 views (last 30 days)
Hello,
I have imported a delimited file in the form of a table. Some columns in the data are dates and I want to replace those dates to numbers so that I can do calculations with them and then reinsert the calculated data back into the matrix. I am using the function number=datenum(inputDate) which works fine but when I try to insert them I get the error:
Error using mainScript (line 21) All input arguments must be tables. It looks like the calculated data are of class "double" and it has to be converted to class "table", can anyone be of help?
I really appreciate, this is holding my whole project back.
  20 Comments
George Papamichael
George Papamichael on 17 May 2021
I don't need year, month, hours minutes and seconds, just number of days. First I have to define an origin for the dates (other than the meaningless 01-Jan-0000). In this example I'd choose 16-May-2021 so the first column will be [-1,0,1]'. Then FM' * FM is a matrix that comes up in multiple linear regression. Take a look at Linear Regression at the last equation of the section "Least-squares estimation and related techniques" where the parameters β are calculated.
Walter Roberson
Walter Roberson on 17 May 2021
Establish an common origin such as jan 1 2021, datetime(), subtract from the datetime of in the table, days() the result. round() if you want. Use that as one numeric column. Use {} indexing to extract the other columns of the table and create an overall numeric array.

Sign in to comment.

Accepted Answer

George Papamichael
George Papamichael on 22 May 2021
I finally found a thing that works and that is to select "Column Vectors" at the import data wizard. Then I worked on each column (I have 8 columns in total, not too bad) and for the time origin of time i used the now() function. This is my code:
clc
n=1000;
origin=floor(now);
var1=VarName1(1:n);
var2=VarName2(1:n);
var3=VarName3(1:n);
var4=VarName4(1:n);
var5=VarName5(1:n);
var6=VarName6(1:n);
var7=VarName7(1:n);
salary=VarName8(1:n);
gender=grp2idx(var2)-1;
dateOfBirth=origin-datenum(var3);
newDates=fixdatenum(var4,var5);
oldNew=grp2idx(var6);
matrix=[ones(n,1) gender dateOfBirth newDates oldNew];
filter=~isnan(newDates);
X=matrix(filter,:);
salaryFiltered=salary(filter);
M = X' * X
observations=length(salaryFiltered)
alpha = X' * salaryFiltered;
beta = M\alpha

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!