Select graph section using ginput

I would like to find the maximum and minimum on a graph for a section of time defined by ginput. I would also like to save this max min y-value in the variables peak_max and peak_min.
I am having trouble writing this correctly in loop form. This is what I have:
for i=(1:8)
y=P(:,i);
plot(time,y)
[x,y]=ginput(2);
pt1=x(1);
pt2=x(2);
peak_max(i)=max(y(pt1:pt2));
peak_min(i)=min(y(pt1:pt2));
end
Basically I want to find the max and min's of each column of data for a specific range and store it. Warnings occur with the storage part.
Any help is appreciated. Thanks.

 Akzeptierte Antwort

Evan
Evan am 10 Jul. 2013
Bearbeitet: Evan am 10 Jul. 2013

0 Stimmen

It looks like you're reassigning y in your code when you call ginput. y should be the ith column of P, but it gets overwritten as the y-coordinate of your click.
Does changing the variable name work? Or, better yet, since you're never using the y value that you click, just discard it:
for i=(1:8)
y=P(:,i);
plot(time,y)
[x,~]=ginput(2);
pt1=x(1);
pt2=x(2);
peak_max(i)=max(y(pt1:pt2));
peak_min(i)=min(y(pt1:pt2));
end

3 Kommentare

Camilla
Camilla am 10 Jul. 2013
I'm still getting this Warning: Integer operands are required for colon operator when used as index.
Additionally I ran my entire script and received this at the end:
Attempted to access x(1); index out of bounds because numel(x)=0.
Error in testmuscle (line 150) pt1=x(1);
Could you explain any of these? Thanks a lot.
Evan
Evan am 10 Jul. 2013
The warning occurs because you have to index your variables with integer values, but the points you click might not be rounded numbers. You should round them to the nearest whole number.
pt1 = round(x(1));
pt2 = round(x(2));
As for the error, can you place a breakpoint in your script just after the ginput line and see what the value of x is?
The values clicked on by the user are unlikely to fall exactly on top of one of your points. Instead, use a logical mask to analyze the points located within the ginput range:
y=P(:,i);
plot(time,y)
[x,~]=ginput(2);
isin = time >= x(1) & time <= x(2);
peak_max(i)=max(y(isin));
peak_min(i)=min(y(isin));

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Exploration finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 10 Jul. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by