MATLAB: How to interactively find the statistics of the zoomed in data in a plot in MATLAB 7.7 (R2008b)

MATLAB

I want to zoom in the plot and find the statistics of data displayed in the plot interactively.

Best Answer

The ability to find the statistics from zoomed in data in a plot is not available in MATLAB 7.7 (R2008b). To work around the issue, try the following:
function stat_plot(n)
figure(1);
x = 1:n;
y = randn(n,1);
hfig = plot(x, y);
setappdata(0, 'x', x);
setappdata(0, 'y', y);
hCM = uicontextmenu;
hMenu1 = uimenu(hCM,'Label','Switch to zoom','Callback','zoom(gcbf,''on'')');
hMenu2 = uimenu(hCM,'Label','Statistics', 'Callback', @myStat);
hPan = pan(gcf);
set(hfig,'UIContextMenu',hCM);
set(hPan,'UIContextMenu',hCM);
pan('on')
end
function myStat(obj, eventdata)
h_children = get(gcf, 'children');
a = get(h_children, 'type');
k = find(cellfun(@(x)(strcmpi('axes', x)), a));
tmp = get(h_children(k), 'Xtick');
x = getappdata(0, 'x');
y = getappdata(0, 'y');
x_min = max(tmp(1), x(1));
x_max = min(tmp(end), x(end));
ydata_selected = y((x_min<=x) & (x<=x_max));
disp('------------------------------');
disp(['Minimum is ', num2str(min(ydata_selected))]);
disp(['Maximum is ', num2str(max(ydata_selected))]);
disp('------------------------------');
end
Run the program with the following command:
stat_plot(n); % where 'n' is the number of data points in the plot
This creates a figure with a plot that has a 'Statistics' UICONTEXTMENU that displays minimum and maximum values from the data at the command prompt. For example, while it is on the PAN mocde, right-click on the plot and select 'Statistics' menu. The minimum and maximum values are shown at the command prompt.
Note that it can only find the min and max values of the data within the 'Xtick' values from the plot. Therefore, if there is a max or min value whose corresponding x-value is outside of the 'xtick' values displayed on the plot, it does not return those min/max values.