【问题标题】:Get data cursor callback in Matlab GUI在 Matlab GUI 中获取数据光标回调
【发布时间】:2017-08-24 20:20:13
【问题描述】:

如何为特定的 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))]};

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

【问题讨论】:

  • 好像datacursormode只支持图形句柄。据我了解,数据游标的行为似乎对图中的所有轴都是通用的。
  • 一定有办法的。它按预期显示所选点,我只是不知道如何设置它的回调,所以我可以做我的事情。
  • 您可以尝试使用hggetbehavior,如here 所述,或使用文件交换dualcursor,支持GUI 内的自定义功能,直至其描述

标签: matlab matlab-guide


【解决方案1】:

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

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

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

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

图形用户界面的标签如下:

  • axesaxes_2d
  • axes:axes_3d
  • pushbuttonpushbutton_2d
  • pushbutton:pushbutton_3d
  • checkbox: checkbox_enable_dc
  • radiobuttonradiobutton1
  • 中间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

希望这会有所帮助,

卡普拉'

【讨论】:

  • 不错的答案!谢谢!
猜你喜欢
  • 2014-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多