# How to get figure data into a variable from mouse click? (alternative to ginput?)

22 Ansichten (letzte 30 Tage)
Mel am 20 Apr. 2013
Hi!
This is what I would like to do:
I have data that contains some real experimental values and -999.00 (no data available for that time) and I want to interpolate across the "missing" data. However, the user may not want to interpolate across the entire time frame that the data is displayed for. So I thought I would lay it out like this:
% t is time and x is the corresponding data
% Plot the data so the user can see it, red if it is a missing data point, blue otherwise:
figure
hold on
for i=1:length(x)
if x(i)==-999.00
plot(t(i),0,'r^')
else
plot(t(i),x(i),'bo')
end
end
hold off
% Get starting values for interpolation
[t1,x1]=ginput(1);
% Interpolate over necessary sections using t1,x1 as the first data point.
The only problem with using ginput is it grabs the value of the user's click, and not the closest plotted data point to the user's click. Is there a way of getting [t1,x1] to be the values of the closest data point?
Thanks!
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Image Analyst am 21 Apr. 2013
Can't you just use the min function?
% Get difference between the t array and where they clicked.
diffs = abs(t - t1);
% Find out where that difference is minimum.
[minDiff, indexAtMin] = min(diffs);
% Now we know the index (the element number)
% so find the value of t at that index.
tClosestToUserClick = t(indexAtMin);
I'm assuming you know how to interpolate just the -999 points, right (since you didn't ask about that specifically)?
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Mel am 21 Apr. 2013
Actually, knowing the index of the point that the user clicked on is better than knowing the value for me. I didn't know you could get the index from min!
And yes, I am okay with the interpolation.

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

per isakson am 21 Apr. 2013
See "Data Cursor" in the on-line help.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Visual Exploration finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by