Suppose I plot a graph on an axis in MATLAB GUIDE. How do I get two static text boxes to display the x and y values of the point my cursor is hovering over? I know this can be done to show the pixel coordinates in the GUI, but when it comes to a plot I am unsure.
MATLAB: How to display the x and y values of the cursor location on plot in GUIDE
cursorguideMATLABplot
Related Solutions
At the time you create a line, you could give it a Callback property. You could probably use the same callback for all of the lines. The Callback would look something like,
function zoom_into_line(hObject, event) line_x = get(hObject, 'XData'); line_y = get(hObject, 'YData'); min_x = min(line_x); max_x = max(line_x); min_y = min(line_y); max_y = max(line_y); zoom_x_min = min_x * .9; zoom_x_max = max_x * 1.1; zoom_y_min = min_y - 50; %you will need to adjust this line
zoom_y_max = max_y + 50; %you will need to adjust this line ax = ancestor(hObject, 'axes'); set(ax, 'XLim', [zoom_x_min, zoom_x_max], 'YLim', [zoom_y_min, zoom_y_max]);
The idea here would be that when you click on the line, the callback runs and automatically zooms the axes to be centered on the x and y coordinates of the line.
This can be done with a little handle graphics. I don't have time right now to make this code perfect (error checking, special cases handling, commenting(!), etc.), but it should give you something of an idea. Run this function then hit the left and right arrow to see what happens.
function [] = data_subplots()% Link data cursors so that they track accross subplots.
% Create the subplots.
% Alternatively, use GET to create s and L from existing plots.
x = 1:.1:10;for ii = 1:3 s(ii) = subplot(3,1,ii); L(ii) = plot(x,sin(x/ii)*ii);end% Now start the work.
for ii = 1:length(s) S.D{ii} = get(L(ii),{'xdata','ydata'}); S.L(ii) = length(S.D{ii}{1}); S.M(ii) = floor(S.L(ii)/2); S.DF = diff(S.D{ii}{1}(S.M(ii):S.M(ii)+1)); % uniform assum
S.I(ii) = S.M(ii); S.T(ii) = text(S.D{ii}{1}(S.M(ii)+2),S.D{ii}{2}(S.M(ii)+2),'here'); set(S.T(ii),'string',{['X: ',sprintf('%3.3g',S.D{ii}{1}(S.M(ii)))];... ['Y: ',sprintf('%3.3g',S.D{ii}{2}(S.M(ii)))]},... 'parent',s(ii),'backgroundco',[.8 .8 0]); set(s(ii),'nextplot','add') S.F(ii) = plot(s(ii),S.D{ii}{1}(S.M(ii)),S.D{ii}{2}(S.M(ii)),'sk'); set(S.F(ii),'markerfacec','b')endset(gcf,'keypressfcn',{@fh_kpfcn,S})function [] = fh_kpfcn(varargin)D = varargin{2}.Key;S = varargin{3};if strcmp(D,'leftarrow') for ii = 1:length(S.T) if S.I(ii)~=1 S.I(ii) = S.I(ii)-1; set(S.F(ii),'xdata',S.D{ii}{1}(S.I(ii)),... 'ydata',S.D{ii}{2}(S.I(ii))) set(S.T(ii),'position',... [S.D{ii}{1}(S.I(ii))+S.DF*2,S.D{ii}{2}(S.I(ii))],... 'string',{['X: ',sprintf('%3.3g',S.D{ii}{1}(S.I(ii)))];... ['Y: ',sprintf('%3.3g',S.D{ii}{2}(S.I(ii)))]}) end endelseif strcmp(D,'rightarrow') for ii = 1:length(S.T) if S.I(ii)~=S.L(ii) S.I(ii) = S.I(ii)+1; set(S.F(ii),'xdata',S.D{ii}{1}(S.I(ii)),... 'ydata',S.D{ii}{2}(S.I(ii))) set(S.T(ii),'position',... [S.D{ii}{1}(S.I(ii))+S.DF*2,S.D{ii}{2}(S.I(ii))],... 'string',{['X: ',sprintf('%3.3g',S.D{ii}{1}(S.I(ii)))];... ['Y: ',sprintf('%3.3g',S.D{ii}{2}(S.I(ii)))]}) end endendset(gcf,'keypressfcn',{@fh_kpfcn,S}) % Update the structure.
Best Answer