While loop to detect 0 slope

3 views (last 30 days)
Neil
Neil on 4 Jun 2014
Commented: Star Strider on 4 Jun 2014
Hello everyone,
so I'm currently trying to make a code that will separate some data. To do so, I came up with a code that I thought would work, and I've tried looking at the what the error message mean, but I cant understand. My code is the follwoing
*g=0; j=0; A=[5 5]; h=0; slope=5; while (g<1785) & (A(1,1)>0) & (h<0.15) h = AverageStrain(1+g:20+g,:); i = AverageStress(1+g:20+g,:); A = polyfit(h,i,1); j = g+10; g = g+20; end
Cycle1UpStrain=AverageStrain(0:j,1);*
The error message i receive is ;
*Subscript indices must either be real positive integers or logicals.
Error in MTSAnalyser (line 43) Cycle1UpStrain=AverageStrain(0:j,1);*
here, the files AverageStress and AverageStraain are 1785x1 variables. I tried doing a linear fit of 20 data points at the time so that when the linear slope calculated is 0 are negative, it stops and extracts the number j.
j would be used to create a variable made of a fraction of AverageStress and AverageStrain, data points 1 to j. (afterwards I would make it something like j to a new variable limit)
Also, my value of A always ends in something like [574, -64], which is weird since I wanted it to stop at a slope equal 0 or smaller, which is the first row, first column of A.
I'm quite new to Matlab, so try not to assume I know a whole bunch.
Thank you for your help! N
  1 Comment
Star Strider
Star Strider on 4 Jun 2014
Formatted code:
g=0;
j=0;
A=[5 5];
h=0;
slope=5;
while (g<1785) & (A(1,1)>0) & (h<0.15)
h = AverageStrain(1+g:20+g,:);
i = AverageStress(1+g:20+g,:);
A = polyfit(h,i,1);
j = g+10;
g = g+20;
end

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 4 Jun 2014
Here’s the problem:
Cycle1UpStrain=AverageStrain(0:j,1);
Zero is not a positive integer. Your (variable or index) ‘j’ is also defined as zero, as best I can see from your code (difficult to read because it’s not formatted).
If you want to find the indices where AverageStrain is equal to zero and one respectively, try this:
AvStn0Idx = find(AverageStrain == 0);
AvStn1Idx = find(AverageStrain == 1);
  2 Comments
Neil
Neil on 4 Jun 2014
Thanks, the mistake was in fact the 0 there. it needed to start at 1. Meanwhile, my A still ends at a value of [571, -644]. Ideas?
Star Strider
Star Strider on 4 Jun 2014
I’m not entirely understand what you’re doing, but I see some problems. One is that h is a vector, not a scalar (at least as I read your code), so if any value of h is less than 0.15, that test will be true.
Consider:
h = [0.1 randi(10,1,5)];
htest = h < 0.15
yields:
htest =
1 0 0 0 0 0
You may want to consider how you’re testing h. What information do you really want from it? First value? Last value? Mean? Norm? Minimum? Maximum?
I would solve the h problem (if it is a problem) first, then see if your while loop runs as it should.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!