MATLAB Answers

How to find and replace the content of an array

4 views (last 30 days)
JOAO LIMA
JOAO LIMA on 8 Apr 2020
Commented: JOAO LIMA on 9 Apr 2020
Hi guys. Sorry to bother you again, but I am new at Matlab.
I have this timestamp array:
'03/07/2019'
'03/07/2019 00:30:00'
'03/07/2019 01:00:00'
'03/07/2019 01:30:00'
'03/07/2019 02:00:00'
'03/07/2019 02:30:00'
And it goes for 10 days (480 values). The first cell of each day is like '03/07/2019', time is missing. I need to find this kind of cell
and add '00:00:00' to the cell content. So, the first cell should be '03/07/2019 00:00;00' instead of '03/07/2019'.
I applied the function cellfun. The length of the desirable cells is 10. I know I have to find the content of these cells and replace it,
but I am writing a loop. I wonder if there is an easier way to do that.
Thank you for your support!

  0 Comments

Sign in to comment.

Accepted Answer

Mohammad Sami
Mohammad Sami on 8 Apr 2020
Edited: Mohammad Sami on 8 Apr 2020
% sometimestamp = {'03/07/2019' '03/07/2019 00:30:00'};
l = cellfun(@length,sometimestamp);
% or
% l = strlength(sometimestamp);
sometimestamp(l==10) = cellfun(@(x)[x ' 00:00:00'],sometimestamp(l==10),'UniformOutput',false);

  1 Comment

JOAO LIMA
JOAO LIMA on 9 Apr 2020
Mohammad Sami , I implemented all the suggestions. ! You guys are great!

Sign in to comment.

More Answers (2)

Bhaskar R
Bhaskar R on 8 Apr 2020
out = cellfun(@(x)datetime(x, 'Format','dd/MM/yyyy HH:mm:ss'), input_cell)

Stephen Cobeldick
Stephen Cobeldick on 8 Apr 2020
>> C = {'03/07/2019' '03/07/2019 00:30:00'};
>> X = cellfun('length',C)==10;
>> C(X) = strcat(C(X),' 00:00:00')
C =
03/07/2019 00:00:00
03/07/2019 00:30:00

  0 Comments

Sign in to comment.