MATLAB Answers

0

How to add values to a column based on time column and its own column?

Asked by Vivian Dinh-Dang on 14 Jul 2017
Latest activity Commented on by Peter Perkins
on 24 Jul 2017
What I'm trying to do is add values to a column based on two conditions. I want to locate the time value based on a value in a different column from the same row. Then I want to continue that value from the different column for the next x minutes that I decide on. I'm not really sure how to be phrasing what I would like so I think it's better if I give what I have now and what I would like to achieve.
My current timetable named E has two columns that look like the 10.44.48 Screen Shot
What I want is to replace the 0 with a 2 in the Interval column for any rows after the current 2 for the next, let's say, 3 minutes following the first 2 value in the Interval column. So I'd end up with the 10.47.08 Screen Shot
I want to be able to add the 2 by just relying on the initial interval value, so that I don't have to search for the time values in my data set for all the different interval values I have. My data set will not necessarily have rows separated by one minute intervals which is why I need to specify the time value as well.
I've been using variables like E.Time to specify my columns.
Please help!

  0 Comments

Sign in to comment.

Tags

2 Answers

Answer by Peter Perkins
on 20 Jul 2017
 Accepted Answer

If your screen shot example is really all you want to do, then it's this:
i = find(tt.Interval > 0);
tt.Interval(i:end) = tt.Interval(i);
But I'm guessing there's more to what you are asking, and your screen shots don't even match your description. You may be asking for this:
i = find(tt.Interval > 0);
tr = timerange(tt.Time(i),tt.Time(i)+minutes(3);
tt(tr,:).Interval = tt.Interval(i);

  2 Comments

Yeah, so I actually ended up writing a code similar to yours:
%
E.Interval(E.Note==4)=2
V=find(E.Interval==2)
X=E.Time(V,1)
Y=timerange(17/07/2017+datetime(X),17/07/2017+datetime(X)+minutes(30))
E.Interval(Y,:)=2
K=max(E.BPM(E.Interval==2,:))
I might try your code instead because it seems simpler and doesn't require me to concatenate a date into my time column. I'm learning MATLAB on my own, so I appreciate that you were able to look past my inconsistencies. Thanks for the help!
update: I tried the timerange but I get a "Attempt to execute SCRIPT timerange as a function" error. Any idea on why?
I image you have something else called timerange on your path. Type which timerange -all.

Sign in to comment.


Answer by Nagarjuna Manchineni on 19 Jul 2017

You can implement your workflow similar to the example below.
data1 = [1;2;3;4;5;6;7;8;9;10];
data2 = [0;0;2;0;0;0;0;0;0;0];
T = table(data1,data2);
for k=1:height(T)
row = T.Row(k);
%if (<your application condition>)
row{1,1} %Accessign data from a table
row{1,2} = 2; %Updating data in a table
%end
end
Also for time data there is a MATLAB function called 'etime' that tells you how much duration is there in between two date vectors.
Refer to the following documentation page for more information on using 'etime':

  1 Comment

Thank you, I'll definitely be using the etime function in the future.

Sign in to comment.