Help Changing Class/Type In A Table
Show older comments
Hi y'all,
How do I change the class/type of values in a column into datetime? They were added as datetime going in. When I go to access the file after and check the class, it indicates the variables are catigorical. Why does this happen?
Thank you!
8 Comments
Walter Roberson
on 28 May 2025
Please show your code of inserting the datetime values into the table, and your code of checking the class of the values.
Kristine
on 30 May 2025
Walter Roberson
on 30 May 2025
date_and_time_cell = strcat(date_only_cell, time_only_cell) ; % combine the date and time cells into one
time_only_cell does not appear to be defined at that point in the code.
Walter Roberson
on 30 May 2025
convert_datetime = datetime(date_and_time_cell, 'InputFormat', in_format_datetime) ; % convert cell data into datetime format/type/class
dt_datetime = datetime(convert_datetime, 'Format', 'MM-dd-yyyy hh:mm:ss') ;
dt_date_only = datetime(convert_datetime, 'Format', 'MM-dd-yyy') ;
The output of convert_datetime is a datetime object.
It is not valid to pass a datetime object as the first parameter of datetime()
Walter Roberson
on 30 May 2025
convert_datetime = datetime(date_and_time_cell, 'InputFormat', in_format_datetime) ; % convert cell data into datetime format/type/class
dt_datetime = datetime(convert_datetime, 'Format', 'MM-dd-yyyy hh:mm:ss') ;
dt_date_only = datetime(convert_datetime, 'Format', 'MM-dd-yyy') ;
If that worked at all, it would replicate the datetime from the filename, and merely adjust the format in two different ways. Setting the Format two different ways on datetime information changes how the datetime information displays but does not change the underlying information . In particular setting the Format on datetime does not remove the hours minutes second information, just sets the h m s to not be displayed. If you want just the date portion, then you should
dateshift(convert_datetime, 'start', 'day')
Cris LaPierre
on 4 Jun 2025
Edited: Cris LaPierre
on 4 Jun 2025
new_table = new_table_useful_data(with_datetime) ;
no_zeroz = remove_zeros(new_table) ;
no_ESD_over_150 = remove_ESD_greater_150(no_zeroz) ;
These 3 data processing functions are also not defined in the shared code either.
Kristine
on 4 Jun 2025
Answers (1)
Cris LaPierre
on 29 May 2025
The best solution would be to fix how your data is imported. Then you don't hvae to convert it.
T.A = datetime(T.A,'InputFormat',infmt)
infmt should be set to match the current data. Since your data is categorical, you likely first need to convert it to a string or numeric data type. We could be more specific if you shared your data and code.
8 Comments
Kristine
on 30 May 2025
Cris LaPierre
on 30 May 2025
@Kristine could you also share a file contianing your data? You can attach it using the paperclip icon.
Kristine
on 30 May 2025
Cris LaPierre
on 30 May 2025
That's fine. We don't need the full file. Just a representative example.
To that end, I'm having difficulty identifying which column should be a datetime. The shared file has 4 columns labeled: roi_number Area Biovolume EquivDiameter
Please share one that includes the column you want to convert to a datetime. If there is any ambiguity to the format, please also let us know what the expected result should be for at least 1 value.
Walter Roberson
on 30 May 2025
The posted function add_datetime_column extracts datetime from the file name, and repeats it once for each row in reference_file
Cris LaPierre
on 31 May 2025
Edited: Cris LaPierre
on 31 May 2025
Thank you. I missed that.
Admittedly, this is greatly simplified, but I think that makes it easier to understand the code.
filename = "D20240603T131852_IFCB196_fea_v2.csv" ;
% read all files
fds = fileDatastore(filename,"ReadFcn",@myFcn,"UniformRead",true);
T = readall(fds)
% Write to a new file
[~, name, ext] = fileparts(filename); % access file info to extract name of file
writetable(T, "new" + name + ext) % Saving files
function out = myFcn(filename)
out = readtable(filename);
[filepath, name, extention] = fileparts(filename); % access file info to extract name of file
T = extractBetween(name,"D","_");
out.Datetime(:) = datetime(T,"InputFormat",'yyyyMMdd''T''HHmmss','Format', 'MM-dd-yyyy hh:mm:ss');
out.DateOnly = out.Datetime;
out.DateOnly.Format = 'MM-dd-yyy';
end
Kristine
on 4 Jun 2025
Cris LaPierre
on 4 Jun 2025
This code does not create categorical values. The code out.DateOnly.Format would throw an error. Are you sure the values are categoricals?
Can you identify where your code diverges from this example? Better yet, share your implementation of this approach.
Categories
Find more on Printing and Saving 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!