在 Matlab GUI 中获取数据游标回调

Get data cursor callback in Matlab GUI

如何为特定的 GUI 轴设置 Matlab GUI 数据光标回调? 我能够 add the data cursor icon in GUI toolbar。我可以在绘制的数据上选择 3D 点,但我需要在数据游标文本中添加更多信息,还需要用它获得的坐标做一些其他事情。

我尝试遵循“How to add additional info to the data cursor?”,但不适用于 GUI。

GUI 轴不是数字,所以我收到此错误:

Error using datacursormode (line 149) Invalid figure handle

我在这里需要同样的东西,但对于 GUI 轴(数字?):

function test_main
% Plots graph and sets up a custom data tip update function
fig = figure('DeleteFcn','doc datacursormode');
X = 0:60;
t = (X)*0.02;
Y = sin(-16*t);
plot(X,Y)
dcm_obj = datacursormode(fig); % tried here "handles.MyFigHandle"
set(dcm_obj,'UpdateFcn',{@myupdatefcn,t})

function txt = myupdatefcn(~,event_obj,t)
% Customizes text of data tips
pos = get(event_obj,'Position');
I = get(event_obj, 'DataIndex');
txt = {['X: ',num2str(pos(1))],...
       ['Y: ',num2str(pos(2))],...
       ['I: ',num2str(I)],...
       ['T: ',num2str(t(I))]};

现在我只能获得默认的数据光标行为,如下图所示:

我没有发现在 GUI 和图形中使用 data cursor 有任何区别。

重新使用您的部分代码,我创建了一个 GUI,其中 checkbox 启用/禁用 datacursormode

GUI 包含两个 axes,其中分别绘制了两条线和一个 3D 表面。

另外三个 radiobuttons 控制将在启用 datacursormode 时生成的文本框中打印的字符串。

GUI的标签如下:

  • axes: axes_2d
  • axes:axes_3d
  • pushbutton: pushbutton_2d
  • pushbuttonpushbutton_3d
  • checkbox: checkbox_enable_dc
  • radiobutton: radiobutton1
  • 中间 radiobutton: radiobutton2
  • radiobutton: radiobutton3

GUI 是这样工作的:

  • 坐标轴中的 pushbuttons 图并启用 checkbox
  • checkbox启用/禁用datacursormode
  • 默认选择左侧radiobutton并设置默认字符串
  • 其他两个radiobutton设置不同的字符串

根据所选 radiobutton,文本框中字符串的第一行将是:

  • 默认字符串
  • 字符串选项一
  • 字符串选项二

要写入文本框中的字符串是在您的 myupdatefcn 中创建的,该字符串已被修改以处理 radiobutton.

选择的选项

注意:为了不限制对myupdatefcn函数版本的修改,我硬编码了t数组。

这是 GUI 的 .m 文件:

function varargout = gui_datacursormode(varargin)
% GUI_DATACURSORMODE MATLAB code for gui_datacursormode.fig
%      GUI_DATACURSORMODE, by itself, creates a new GUI_DATACURSORMODE or raises the existing
%      singleton*.
%
%      H = GUI_DATACURSORMODE returns the handle to a new GUI_DATACURSORMODE or the handle to
%      the existing singleton*.
%
%      GUI_DATACURSORMODE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI_DATACURSORMODE.M with the given input arguments.
%
%      GUI_DATACURSORMODE('Property','Value',...) creates a new GUI_DATACURSORMODE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before gui_datacursormode_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to gui_datacursormode_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help gui_datacursormode

% Last Modified by GUIDE v2.5 02-Apr-2017 17:45:45

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @gui_datacursormode_OpeningFcn, ...
                   'gui_OutputFcn',  @gui_datacursormode_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before gui_datacursormode is made visible.
function gui_datacursormode_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to gui_datacursormode (see VARARGIN)

% Choose default command line output for gui_datacursormode
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes gui_datacursormode wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = gui_datacursormode_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton_2d.
function pushbutton_2d_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_2d (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Plot two lines in the first axes
t=0:.1:2*pi;
plot(handles.axes_2d,t,sin(t),'r');
hold(handles.axes_2d,'on')
plot(handles.axes_2d,t,cos(t),'b');
% Enable the checkbox that will set datacorsormode on
handles.checkbox_enable_dc.Enable='on';


% --- Executes on button press in pushbutton_3d.
function pushbutton_3d_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_3d (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Plot a 3D graph in the second axes
axes(handles.axes_3d);
peaks

% Enable the checkbox that will set datacorsormode on
handles.checkbox_enable_dc.Enable='on';


function txt = myupdatefcn(~,event_obj,t)
% Customizes text of data tips

% Get the handles of the GUI to access to the radiobuttons
my_guidata=guidata(gcf);
% Define the additional string to be written
if(my_guidata.radiobutton1.Value)
   str='DEFAULT STRING ';
elseif(my_guidata.radiobutton2.Value)
   str='STRING OPTION ONE ';
else
   str='STRING OPTION TWO ';
end

% Get the datacursor data
pos = get(event_obj,'Position');
I = get(event_obj, 'DataIndex');
% Create the whole string to be written
txt = {[str], ...
       ['X: ',num2str(pos(1))],...
       ['Y: ',num2str(pos(2))],...
       ['I: ',num2str(I)],...
       ['T: ',num2str(t(I))]}


% --- Executes on button press in checkbox_enable_dc.
function checkbox_enable_dc_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_enable_dc (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% If the checkbox is set
if(hObject.Value)
   %  enable the radiobuttons that control the string to be written
   handles.radiobutton1.Enable='on';
   handles.radiobutton1.Value=1;
   handles.radiobutton2.Enable='on';
   handles.radiobutton3.Enable='on';
   % Create the datacursormode object
   dcm_obj = datacursormode(gcf)
   t=rand(1,10000);
   set(dcm_obj,'DisplayStyle','datatip',...
      'SnapToDataVertex','off','Enable','on', ...
      'UpdateFcn',{@myupdatefcn,t})
else
   % If the checkbox is not set, disable the datacursormode
   datacursormode 'off'
end


% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton1

% Toggle the other radiobuttons
handles.radiobutton2.Value=0
handles.radiobutton3.Value=0

% --- Executes on button press in radiobutton1.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton1

% Toggle the other radiobuttons
handles.radiobutton1.Value=0
handles.radiobutton3.Value=0


% --- Executes on button press in radiobutton1.
function radiobutton3_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton1

% Toggle the other radiobuttons
handles.radiobutton1.Value=0
handles.radiobutton2.Value=0

希望这对您有所帮助,

Qapla'