MATLAB Answers

Time conversion in a table

1 view (last 30 days)
Bluegin
Bluegin on 1 Sep 2016
Commented: Bluegin on 2 Sep 2016
Hi all, Let's say I have a table and one of my variable is a mix of numbers and NaNs, like so: [1010, NaN, 1256, NaN, 1345...]. Now, I want to convert the numbers into hours-minute format (HH:MM) and keep the same number of rows so the output looks like this : [10:10, NaN, 12:56, NaN, 13:45 ...]. Any ideas? Thanks again, you guys are great!

  0 Comments

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 1 Sep 2016
a=[1010, NaN, 1256, NaN, 1345]
idx=isnan(a)
a(idx)=0
out=arrayfun(@(x) datestr(datenum(sprintf('%04d',x),'HHMM') ,'HH:MM') ,a,'un',0)
out(idx)={nan}

  1 Comment

Bluegin
Bluegin on 2 Sep 2016
Huh. Pretty neat, Mr. Abdelmalek

Sign in to comment.

More Answers (0)

Sign in to answer this question.