在MatlabGUI中获取数据游标回调
如何为特定GUI轴设置Matlab GUI数据光标回调? 我能做到。我可以在打印的数据上选择3D点,但我需要在数据光标文本中添加更多信息,还需要对它获得的坐标进行其他处理 我试图遵循“”,但不适用于GUI GUI轴不是图形,因此我得到以下错误: 使用datacursormode时出错(第149行) 无效的数字句柄 这里我需要相同的,但对于GUI轴(图?):在MatlabGUI中获取数据游标回调,matlab,matlab-guide,Matlab,Matlab Guide,如何为特定GUI轴设置Matlab GUI数据光标回调? 我能做到。我可以在打印的数据上选择3D点,但我需要在数据光标文本中添加更多信息,还需要对它获得的坐标进行其他处理 我试图遵循“”,但不适用于GUI GUI轴不是图形,因此我得到以下错误: 使用datacursormode时出错(第149行) 无效的数字句柄 这里我需要相同的,但对于GUI轴(图?): 功能测试\u main %绘制图形并设置自定义数据提示更新功能 图=图('DeleteFcn','doc datacursormode');
功能测试\u main
%绘制图形并设置自定义数据提示更新功能
图=图('DeleteFcn','doc datacursormode');
X=0:60;
t=(X)*0.02;
Y=sin(-16*t);
绘图(X,Y)
dcm_obj=数据游标模式(图);%在这里试过“把手,我的手指”
集合(dcm_obj,'UpdateFcn',{@myupdatefcn,t})
函数txt=myupdatefcn(~,事件_obj,t)
%自定义数据提示的文本
pos=获取(事件对象,“位置”);
I=获取(事件对象,“数据索引”);
txt={['X:',num2str(位置(1))],。。。
[Y:',num2str(位置(2))],。。。
[I:',num2str(I)],。。。
['T:',num2str(T(I))]};
目前,我只能获得默认的datacursor行为,如下图所示:
我没有发现在GUI和图形中使用
数据光标之间有任何区别
在使用部分代码时,我创建了一个GUI,其中有一个复选框
启用/禁用datacursormode
GUI包含两个轴
,其中分别绘制了两条直线和一个3D曲面
还有三个单选按钮
控制启用datacursormode
时生成的文本框中打印的字符串
GUI的标签如下所示:
- 左
轴
:轴_2d
- 右
轴
:轴_3d
- 左
按钮
:按钮_2d
- 右
按钮
:按钮\u 3d
checkbox
:checkbox\u enable\u dc
- 左
radiobutton
:radiobutton1
- 中间的
radiobutton
:radiobutton2
- 右键
radiobutton
:radiobutton3
GUI的工作方式如下:
按钮
在轴上绘图并启用复选框
复选框
启用/禁用datacursormode
- 默认情况下,选择左侧的
单选按钮
,并设置默认字符串
- 另外两个
单选按钮
设置不同的字符串
根据所选的单选按钮
,文本框中字符串的第一行将为:
- 默认字符串
- 字符串选项一
- 字符串选项二
要在文本框中写入的字符串是在您的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'似乎datacursormode只支持图形句柄。从我的理解来看,数据光标的行为似乎对图形上的所有轴都是通用的。一定有办法。它按预期显示所选的点,我只是不知道如何设置它的回调,这样我就可以完成我的工作。您可以尝试使用hGetBehavior
,如前所述,或者使用文件交换,它支持GUI中的自定义函数,直到它的描述。回答不错!非常感谢。