Why do I keep getting this error?
Show older comments
The error message is :
Error using line
Vectors must be the same length.
Error in filter_and_star_and_final (line 37)
line([t(t_startx), t(t_startx)], ylim, 'Color', 'r', 'LineWidth', 2); % Put a red line there.
I have attache my code and the file that I am trying to use.
I would appreciate any help
Answers (1)
Adam Danz
on 12 Mar 2019
My guess is that ylim produces a [1 by 2] vector but [t(t_startx), t(t_startx)] does not produce a [1 by 2] vector.
If that's the case,
timePoint2 = FindTimePoint2(x); % I bet timePoint2 has more than 1 value!
t_startx= timePoint2;
13 Comments
Adam Danz
on 12 Mar 2019
Instead of getting the value from the command window, just remove the semicolon at the end of the lines that produce 'timePoint2' so your code will display their values upon execution.
timePoint2 = FindTimePoint2(x) %no ending ';'
"i got the value in the command window yo double check if there is anything calculated for timePoint2. and it says 0x1 double cell
but i dont know how to make it into a double cell and use one value for timePoint2 and the same thing for timepoint5 too.
the code works for some datasets, and doesnt work for some datasets...i dont know how to correct it..."
Do you know how to use debug mode? I suggested you step through your code, line by line, and understand what each line is supposed to do and check that it's doing that. It will take some time (whether I do that or you do that) and since it's your code, you're the one who will benefit the most from this exercise. My guess is that the problems is within the FindTimePoint2() function. Be on the look out for NaNs or Infs, too.
Lala0099
on 13 Mar 2019
Lala0099
on 13 Mar 2019
Adam Danz
on 13 Mar 2019
You need to use debug mode. It's really simple and quick to learn and the benefit will be long lasting.
Open your code, put a 'break' in your code toward the top of your code by clicking on the left margin. A red dot will appear on that line.
Then run your code. The execution will stop at the red dot. Then press F10 to step through your code line by line to understand what's going on. That's how to debug code.
Lala0099
on 13 Mar 2019
Adam Danz
on 13 Mar 2019
Just so I understand, you're saying that alll of the following code is executed except for the line with the "*********" ?
%function output = myfunction(input)
function timePoint2 = FindTimePoint2(signal)
% Define timepoint1, where the signal is known to be stable.
timePoint1 = 70; % Index, not absolute time.
% Find the mean and standard deviation in the first fifty samples.
meanSignal = mean(signal(1:timePoint1));
sdSignal = std(signal(1:timePoint1));
% Find where the signal deviates from the mean the most.
meanDeviationFromMean = abs(signal - meanSignal);
[maxDeviation, timePoint3] = max(meanDeviationFromMean); % *********
timePoint3 = find(meanDeviationFromMean > 10*sdSignal);
% Find the last place in the signal, between timePoint1 and timePoint3
% where the signal equals the mean. This will be timePoint2.
signal2 = signal; % Make copy.
% Make signal mean before timePoint1 and after timePoint3 so we will
% find the timePoint2 only within that section.
signal2(1 : timePoint1) = meanSignal;
signal2(timePoint3 + 1 : end) = meanSignal;
% Now find timePoint2
timePoint2 = find(meanDeviationFromMean(1:timePoint3) <= 6*sdSignal, 1, 'last');
end
That sounds very fishy. Lines aren't merely skipped. Try closing and re-opening the file. Also execute this line, just in case.
rehash path
Does that solve your problem?
Lala0099
on 14 Mar 2019
Lala0099
on 14 Mar 2019
Categories
Find more on Logical 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!