Having issue using the command of readtable

1 view (last 30 days)
Adnan Jafar
Adnan Jafar on 12 May 2021
Answered: Sahil Jain on 11 Aug 2021
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 Comment
dpb
dpb on 13 May 2021
"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.

Sign in to comment.

Answers (1)

Sahil Jain
Sahil Jain on 11 Aug 2021
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.

Categories

Find more on Data Type Conversion 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!