Having issue using the command of readtable

1 次查看(过去 30 天)
Hi folks,
I am using readtable command to read the colums ('timestamp', 'Record Type', 'Value') in the attached file.
rawTable=readtable(path) % where path is the locaton of file in my computer
However, when I read variable rawTable.timestamp, it gives me the values in datetime format. However, I want these values in cell array format {'dd:mm:yy HH:mm'}.
Anyhelp would be much appreciated. Thank you.
Note: In original file, I was having values in cell array format. When i edited the .csv file, then I was getting 'timestamp' valus in datetime format.
I tried using different tricks like
im=detectImportOptions(path, 'NumHeaderLines', 0);
im=setvartype(im, 1, 'char');
rawTable=readtable(path,im);
With this trick, I am getting time in string array but in different format e.g. {10/01/2020 10:20'}, {'10:01:20 10:20'}. I would like to have timestamp values in unique string array format {'dd:mm:yy HH:mm'}.
  1 个评论
dpb
dpb 2021-5-13
"I want these values in cell array format {'dd:mm:yy HH:mm'}. "
May I be so bold as to ask why would you want datetime data as cellstr instead of the natural datetime?
You can always generate the output format however you want it for whatever purpose besides using it as time; but you completely lose the ability to manipulate, search, calculate, plot, ... anything else in that form.
Some background on the application might help to understand...
You can, of course, use an import options object and specifiy the way you want each variable interpreted on input if you're adamant in going that route.

请先登录,再进行评论。

回答(1 个)

Sahil Jain
Sahil Jain 2021-8-11
According to my understanding of the question, you want to convert the ‘timestamp’ column of your table from datetime format to cell array format. The code below should help you achieve this.
rawTable = readtable('data.csv'); % read the table
cell_array = convertStringsToChars(cellstr(rawTable.Timestamp, 'dd:MM:yy HH:mm')); % convert datetime to char cell array
rawTable.Timestamp = cell_array; % add the cell array to the table
With this approach, you can format the timestamp string as you want by changing the 'dd:MM:yy HH:mm' string.

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by