MATLAB Answers

facing problem in loop

1 view (last 30 days)
Ali Asghar
Ali Asghar on 1 Aug 2019
Edited: Bob Nbob on 1 Aug 2019
I am facing problem in below program.....
buttLoop3 is data signal having 30000x4 values
I want to find zero crossing on buttLoop3 in such a way that ti(k) return values of four different signal crossing zero value in 4 columns.....
for i = 1:4
y(i) = buttLoop3(:,i) ; % filtered signal 7:05pm 31-7-19
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
xi = zci(y(i) ); % zero crossing indices command
for k = 1:numel(xi)-1
ti(k) = interp1(y(i)([xi(k) xi(k)+1]), t([xi(k) xi(k)+1]), 0); % Interpolate To Find ‘t’ At ‘y=0 ’


Show 5 older comments
Ali Asghar
Ali Asghar on 1 Aug 2019
I posted entire error message which i get after run it.
although line 6 is ti(k) = interp1(y(i)([xi(k) xi(k)+1])); % Interpolate To Find ‘t’ At ‘y=0 ’
I request if u write the whole code so i can run it and find my mistake.
Thank you
Bob Nbob
Bob Nbob on 1 Aug 2019
The new error is with this: y(i)([xi(k) xi(k)+1]).
If we simplify the second index, you having something of the form y(a)(b), which Matlab is not able to make sense of. The use of parentheses indicates that you are looking to call the entire element indicated by the values within the index, and so you can only use () indexing as the final index call. If you have a cell array, which can contain other matrices, it is possible to call the contents of a cell with curly braces {}, and then call elements within. If your y variable is a cell array, then the calling would look like this, y{a}(b). Notice that the paretheses are around the last index being called.
Without knowing what any of your variables are, or what exactly you're trying to do with this line, I cannot give you an exact solution on how to fix it. I can guess from the use of interp1 command that you have simply forgotten to include a comma after y(i), because interp1 requires three inputs, and were not intending to create a double index at all. If that is not the case, however, I suggest making the necessary adjustments to your indexing of y so that you are properly calling the elements.
Unfortunately, I cannot fully guess as to what your third input should be for interp1, because you only have two listed. If you know however, you should enter them in the order of interp1(X,Y,x1), where X is the independant variable array, Y is the dependant variable array, and x1 is the independant value you would like to interpolate to.
Guillaume on 1 Aug 2019
Note that the first comment by Walter had already pointed out that the interp line was invalid syntax.
Once that syntax error is fixed, the program will still not run since as also pointed by Walter:
y(i) = something with more than 1 element
is a runtime error.
As Walter said, using y everywhere instead of y(i) would solve both problems.
Also, the usefulness of the creation of the anonymous function inside the loop is questionable as it is used only once immediately after being created. May as well use the expression directly on y and not bother with the function call... or create the function just once outside the loop.

Sign in to comment.

Answers (0)