Include missing elements in array (matlab)

1 view (last 30 days)
Nia
Nia on 5 Aug 2015
Edited: Nia on 10 Aug 2015
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

Madhav Rajan
Madhav Rajan on 7 Aug 2015
I understand that you want to include the missing timestamps using [] in your newtable. You can make use of the 'intersect' function to compare the durations in the complete time array with the durations in your table. You can use the below example for your reference:
%%Initalization
% This creates the Time and values array
times = duration(0, 1, 0:20)';
vals = rand(21,1);
Perform intersection with the new duration variable
newtimes = duration(0, 0, 0:120)';
result = cell(length(newtimes),1);
%get the duration, the duration's index in times and the duration's index
%in newtimes
[dur, itimes, inewtimes ] = intersect(times,newtimes);
v=num2cell(vals(itimes));
[result{inewtimes}] = deal(v{:});
newtable = table(newtimes, result);
In the above example the first section initializes the times and vals similar to your 'mytable'. The second section creates an array of newtimes which contains more duration values than the first times variable. You then find the intersection and its indices between the two time variables and then assign the values to the corresponding indices in the newtimes variable. With the indices available, you would just have to do a cell array assignment as shown above.
You can refer the following link for more information on the 'intersection' function:
Hope this helps.
  1 Comment
Nia
Nia on 10 Aug 2015
Edited: Nia on 10 Aug 2015
Thanks!, it worked. But in my case as I had a cell array and a duration array, I also had to convert my Complete_time_array duration array into a cell array to be able to use the intersect function:
b=char(Complete_time_array);
[dur, itimes, inewtimes ] = intersect(mytable.Times,b);
Otherwise I was getting the following error: "Error using duration/intersect. Comparison is not defined between cell and duration arrays". I think both elements must be of the same class before they can be intersected.

Sign in to comment.

More Answers (0)

Categories

Find more on Tables 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!