필터 지우기
필터 지우기

Casting cell array of cells to something workable

조회 수: 5 (최근 30일)
supernoob
supernoob 2018년 4월 18일
댓글: supernoob 2018년 4월 19일
Hi there, I have an Nx1 cell (named 'intervals') which contains cell arrays. For example:
K>> intervals(1:5,1)
ans =
5×1 cell array
{'2018-03-2610:25:51 PM'}
{'2018-03-2610:25:51 PM'}
{'2018-03-2610:25:51 PM'}
{'2018-03-2610:25:52 PM'}
{'2018-03-2610:25:52 PM'}
The cell arrays are supposed to contain dates but as you can see they are improperly formatted - I need to add a space between the day and hour to be able to convert this into something useful. The cell I have is very very long (~1500000x1), so I don't want to do this with a for loop. Is there a smart way to add a space in each cell array at the correct spot? Another option is to cast this cell array of cells into a matrix of strings, but every variation of cell2mat I have tried gives me a 1498393x21 char.
My desired output is an array of datetimes
2018-03-26 10:25:51 PM
2018-03-26 10:25:51 PM
2018-03-26 10:25:51 PM
2018-03-26 10:25:52 PM
2018-03-26 10:25:52 PM
My current code will take forever, because the actual text file I'm working with is so huge:
function dates = timestamp_correction()
filename = 'intervals.txt';
fileID = fopen(filename);
intervals = textscan(fileID, '%s', 'delimiter', '\t');
intervals = intervals{1,1};
for i=1:numel(intervals)
dates(i) = datetime(intervals{i,1}, 'InputFormat', 'yyyy-MM-ddhh:mm:ss a');
end
dates = dates';
end
trying
datetime(intervals{:}, 'InputFormat', 'yyyy-MM-ddhh:mm:ss a');
throws an error, it wants it to be an array first but I can't get that to work :(
I've attached a much smaller text file which is a truncated version of the bigger one I am working with.
Thanks!
  댓글 수: 4
Walter Roberson
Walter Roberson 2018년 4월 18일
"I need to account for the fact that the day might have one or two digits"
So for example, 2018-03-210:25:51 PM would be possible for 2nd of the month at 10:25:51 PM ?
If so then what happens for times between 1 and 9 ? Is the leading 0 omitted for them, leading to, for example, 2018-03-261:25:51 PM for 1:25:51 PM on the 26th?
If so, then can both end up with leading zeros removed, such as 2018-03-21:25:51 PM for 1:25:51 PM on the 2nd ?
If both can have leading zeros removed then you cannot tell whether 2018-03-211:25:51 PM is 1:25:51 PM on the 21st, or is 11:25:51 PM on the 2nd.
supernoob
supernoob 2018년 4월 18일
You know what, you're right. And it turns out the leading zero is not omitted, so luckily this is not a concern. I've edited my question, thanks for pointing this out.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2018년 4월 18일
reformatted_intervals = cellfun(@(S) [S(1:10) ' ' S(11:end)], intervals, 'uniform', 0)

However since your purpose is to convert to another form, you can instead skip the reformatting and use

intervals_dt = datetime(intervals, 'InputFormat', 'yyyy-MM-ddhh:mm:ss a');
  댓글 수: 1
supernoob
supernoob 2018년 4월 19일
Thanks so much Walter. I'm embarassed I couldn't come up with this on my own. I kept trying to convert the cell array first before calling datetime on it, I should have taken the most straightforward approach! That cellfun but will prove to be handy for other work too. Cheers!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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