Include missing elements in array (matlab)
Show older comments
I have a Matlab table called 'mytable' with two columns (Times and value). See example extract below:

I would like to detect and include the missing time stamps in the Times column and assign an empty value in the following way:

I have only figured out how to do it using the following code:
%%Creates a complete duration array
A=duration(00,00,00);
B=duration(23,59,59);
Complete_time_array=linspace(A,B,86400);
%%Compare Complete_time_array with mytable.Times values and include the missing time stamps
Times=repmat({''},length(Complete_time_array),1);
value=repmat({''},length(Complete_time_array),1);
for t=1:length(Complete_time_array)
if any(strcmp(char(Complete_time_array(1,t)),mytable.Times))==1
index=find(strcmp(char(Complete_time_array(1,t)),mytable.Times));
Times{t,1}=mytable.Times(index,1);
value{t,1}=mytable.value{index,1};
else
Times{t,1}=char(Complete_time_array(1,t));
value{t,1}='';
end
end
mynewtable=table(Times,value);
However, when working with tables that have thousands of rows, this code takes too long despite having used the 'any' and 'find' matlab functions. Does anybody know how to achieve this goal more efficiently?
Accepted Answer
More Answers (0)
Categories
Find more on Logical 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!