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!