How to convert class double to class table ?
Afficher commentaires plus anciens
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 commentaires
Walter Roberson
le 12 Mai 2021
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
le 12 Mai 2021
Walter Roberson
le 12 Mai 2021
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
le 12 Mai 2021
Walter Roberson
le 12 Mai 2021
matrix = table(dateOfBirth, datesFixed, oldNew, salary);
George Papamichael
le 12 Mai 2021
Walter Roberson
le 12 Mai 2021
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
le 12 Mai 2021
George Papamichael
le 15 Mai 2021
Image Analyst
le 15 Mai 2021
@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
le 15 Mai 2021
Modifié(e) : Walter Roberson
le 16 Mai 2021
Walter Roberson
le 16 Mai 2021
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
le 16 Mai 2021
Walter Roberson
le 16 Mai 2021
Okay then: you should use
rows2vars(fm)
to transpose the fm table() object.
George Papamichael
le 16 Mai 2021
Walter Roberson
le 16 Mai 2021
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
le 17 Mai 2021
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
le 17 Mai 2021
Walter Roberson
le 17 Mai 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.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Tables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
