Why do I keep getting this error?

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)

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

Lala0099
Lala0099 on 12 Mar 2019
Edited: Lala0099 on 12 Mar 2019
when I just look for the value of timePoint2 in the comman window it comes back as 0
I think it does not calculate is
But then again, the first dataset I did it with it worked....
I just dont get it
i attached the data set it works with
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 ';'
Adam Danz
Adam Danz on 12 Mar 2019
Edited: Adam Danz on 12 Mar 2019
"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.
I'd be happy to help if you get stuck and can provide a minimal working example of the problem.
I dont know how to use the debug mode, but I have ben trying to find the error.
I had to change a few values and now it works better. But there is still a problem
And I think I found the reason.
So within my functions, it does not find the timePoint3.
[maxDeviation, timePoint3] = max(meanDeviationFromMean);
So this and the line that uses this information is not executed. Why is it not executed?
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...
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.
I have done it, but it just skips the line that is not executed, the line that i have problem with. I dont understand why the input into that line is not good...
[maxDeviation, timePoint3] = max(meanDeviationFromMean);
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
yes. That line and this other line in the bottom.
(But i think this line is not executed, because that other line is not executed..)
signal2(timePoint3 + 1 : end) = meanSignal;
signal2(1 : timePoint1) = meanSignal;
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?
Well I think the problem was that somehow the dimensions did not match the requirements, and the find() command returned a 0x1 ouput.
So i just added a +1 to the code, so the cell would not be empty. and I think it might have solved the problem. But i dont know really.
I dont know why it returns the 0x1 . ~
Adam Danz
Adam Danz on 14 Mar 2019
Edited: Adam Danz on 25 Mar 2019
Be careful. Just because the error message no longer shows doesn't meet the change you meet corrected the real problem.
okay, thank you .
I dont know how to fix the real problem.

Sign in to comment.

Asked:

on 12 Mar 2019

Edited:

on 25 Mar 2019

Community Treasure Hunt

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

Start Hunting!