Having issue using the command of readtable

조회 수: 1 (최근 30일)
Adnan Jafar
Adnan Jafar 2021년 5월 12일
답변: Sahil Jain 2021년 8월 11일
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