How to store data in multiple matrix from table

2 views (last 30 days)
I have one time series data in a table of 260000 x 3. where col_1 = parameter_one, col_2 = parameter_two, col_3 = time.
I want to collect the data when there is change in parameter an then stored inside a different matrix.
example: If there is change in parameter for 9 times then it should stored the data of that parameter in 9 different arrays where. array(1) = parameter and array(2) = time data.
function slopeplot(DATA_1)
data = table2array(DATA_1);
thrust = ones(1,1);
c2 = 1;
for c = 1:9
for i = c2:length(data)
thrustslope = (data(i+1,1)-data(i,1))/(data(i+1,3)-data(i,3));
if thrustslope >= abs(0.2)
thrust(i,1) = data(i,1);
thrust(i,2) = data(i,3);
elseif thrustslope == 0
c2 = i;
break;
else
continue;
end
end
subplot(3,3,c);
plot(thrust);
end
end
My code is working with zero error; however, i am not getting plots as well as array varibles in the workspace.
is there anything wrong with my logic?
i ran the code line by line; however, unable to find the bug.
Kindly help me.

Answers (1)

Walter Roberson
Walter Roberson on 8 Dec 2022
if thrustslope >= abs(0.2)
It is possible that is never true.
elseif thrustslope == 0
It is possible that is happening very early.
thrustslope = (data(i+1,1)-data(i,1))/(data(i+1,3)-data(i,3));
It is possible that is infinite, if the denominator turns out 0.
Note that as you loop around, you never reset thrust so if for one c value the loop continues (say) to i = 20, and the next time only to i = 3, then whatever you wrote into thrust after 3 would still be there.
  2 Comments
Walter Roberson
Walter Roberson on 8 Dec 2022
for c = 1:9
for i = c2:length(data)
%...
elseif thrustslope == 0
c2 = i;
break;
Consider c = 1. Suppose (for simplicity) you do not happen to encounter any if thrustslope >= abs(0.2) cases before you encounter thrustslope==0 . Suppose you encounter that case when i is (say) 4. So you set c2 = 4 and you break the for i loop. You subplot() and you plot(thrust). Then you loop back in the for c loop to execute the loop with c = 2. Now you for i = c2:length(data) where c2 was just set to 4. So i starts with 4. Now take a closer look at
thrustslope = (data(i+1,1)-data(i,1))/(data(i+1,3)-data(i,3));
That involves i, but does not involve c . i is starting at c2, which is the same place that last time through the for c loop calculated thrustslope == 0. data did not change, i is the same thing it was when you break because thrustslope was 0... clearly if you break the loop last time because thrustslope was 0, then again this time you are going to calculate thrustslope = 0, but immediately this time. So you are going to set c2 = i which is the same as it already was, you are going to plot() the same as you did last time, and you go on to c = 3 which by the same logic is going to immediately have thrustslope = 0 and so on.
If you examine your code you can see that you are plotting exactly the same thing every time.
Unfortunately you did not document what you are trying to achieve.
I suspect that you might be trying to find up to 9 different segments separated by thrust 0, plotting each one separately. If so then you should initialize c2 = 0 and use for i = c2+1:length(data)
But possibly you are trying to find up to 9 different segments with thrust > 0.2 and plotting each segment, but stopping the search all-together the first time you find thrust 0 ??
Question:
if thrustslope >= abs(0.2)
what is the point of taking abs() of a positive constant?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!