Help Changing Class/Type In A Table
古いコメントを表示
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 件のコメント
Walter Roberson
2025 年 5 月 28 日
Please show your code of inserting the datetime values into the table, and your code of checking the class of the values.
Kristine
2025 年 5 月 30 日
Walter Roberson
2025 年 5 月 30 日
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
2025 年 5 月 30 日
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
2025 年 5 月 30 日
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
2025 年 6 月 4 日
編集済み: Cris LaPierre
2025 年 6 月 4 日
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
2025 年 6 月 4 日
回答 (1 件)
Cris LaPierre
2025 年 5 月 29 日
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 件のコメント
Kristine
2025 年 5 月 30 日
Cris LaPierre
2025 年 5 月 30 日
@Kristine could you also share a file contianing your data? You can attach it using the paperclip icon.
Kristine
2025 年 5 月 30 日
Cris LaPierre
2025 年 5 月 30 日
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
2025 年 5 月 30 日
The posted function add_datetime_column extracts datetime from the file name, and repeats it once for each row in reference_file
Cris LaPierre
2025 年 5 月 31 日
編集済み: Cris LaPierre
2025 年 5 月 31 日
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
2025 年 6 月 4 日
Cris LaPierre
2025 年 6 月 4 日
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.
カテゴリ
ヘルプ センター および File Exchange で Printing and Saving についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!