How to convert class double to class table ?
이전 댓글 표시
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
Walter Roberson
2021년 5월 12일
What is your code on line 21 -- the code that tries to reinsert the calculated data back into the matrix?
Some columns in the data are dates
Do you mean character vectors, or do you mean datetime() objects ? It is recommended to stop using datenum() and only use datetime() and duration() related functions.
George Papamichael
2021년 5월 12일
Walter Roberson
2021년 5월 12일
For example you can
dt = datetime('now');
dt12 = dateshift(dt, 'start', 'hour') - hours(12);
I was hoping you would post the code for your line 21 so we could have a look at what you were doing.
George Papamichael
2021년 5월 12일
Walter Roberson
2021년 5월 12일
matrix = table(dateOfBirth, datesFixed, oldNew, salary);
George Papamichael
2021년 5월 12일
Walter Roberson
2021년 5월 12일
datesFixed=fixdatenum(sal{:,4},sal{:,5});
That hints to me that you are putting together a date and time field. If so then the result is not going to be an integer.
Anyhow... your current format affects how tables display.
George Papamichael
2021년 5월 12일
George Papamichael
2021년 5월 15일
Image Analyst
2021년 5월 15일
@George Papamichael, you keep forgetting to attach your data. Please attach the file and your code for reading it in. That way others (who don't have the Crystal Ball Toolbox like Walter) could try to help you.
George Papamichael
2021년 5월 15일
편집: Walter Roberson
2021년 5월 16일
Walter Roberson
2021년 5월 16일
I do not see where you have created a matrix X at all?
You created a table() object that only has numeric values inside it, but is still a table() object.
You could extract the numbers and transpose that
fm{:,:}.'
but it would not be a table() object
George Papamichael
2021년 5월 16일
Walter Roberson
2021년 5월 16일
Okay then: you should use
rows2vars(fm)
to transpose the fm table() object.
George Papamichael
2021년 5월 16일
Walter Roberson
2021년 5월 16일
What you should do at this point is give up. You will never be able to do mathematical operations on a table.
If only you had a numeric array instead of a table...
George Papamichael
2021년 5월 17일
A = datetime('now')-days([2 1 0].');
B = [4 7 9].'
FM = table(A,B)
What result would you expect from taking FM' * FM ? What does it mean to multiply a date by 7 ?
George Papamichael
2021년 5월 17일
Walter Roberson
2021년 5월 17일
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.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
