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 で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
