【问题标题】:Matlab GUI axes extract position from datacursorMatlab GUI 轴从数据游标中提取位置
【发布时间】:2015-01-18 21:13:10
【问题描述】:

希望任何人都可以帮助我。

我尝试在我的 GUI 中加载和显示图像。 之后我激活datacursormode。现在我想从我的数据游标对象中提取位置并将信息保存到全局变量中以处理这些数据。

这是我的代码:

function varargout = myGui(varargin)

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
               'gui_Singleton',  gui_Singleton, ...
               'gui_OpeningFcn', @myGui_OpeningFcn, ...
               'gui_OutputFcn',  @myGui_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 myGui is made visible.

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

path = uigetimagefile;
img = imread(path);

axes(handles.axes1);

imshow(img);

dcm_obj = datacursormode(handles.figure1);
datacursormode on;
set(dcm_obj,'UpdateFcn', @myupdatefcn );


handles.pos = hObject;
handles.pos = get(0,'userdata');
set(handles.txt1,'String',num2str(handles.pos));
guidata(hObject, handles);


% --- Outputs from this function are returned to the command line.
function varargout = myGui_OutputFcn(hObject, eventdata, handles) 

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

在一个额外的 .m 文件中,我有 'myupdatefcn' 像 thi

function txt = myupdatefcn(~, event_obj)
posi = event_obj.Position;
disp(['You clicked X:',num2str(posi(1)),', Y:',num2str(posi(2))]);
txt = {'Point to Compute'};
set(0,'userdata',posi(1));
end

谁能解释我如何做到这一点?

谢谢

【问题讨论】:

  • 我在this answerthis answer 中解释了如何做到这一点。如果你看第二个答案,直接进入“第 2 章。以编程方式导出”。
  • 感谢您的帮助。我有点不同。现在它工作了一半。我之前编辑了我的帖子以展示我的新实现。我的实际问题是 gui 没有更新。
  • 你想“检测到点击图片”,然后报告位置还是创建一个数据游标,让用户定位它然后报告位置?
  • 第二个。创建一个数据游标,让用户定位它然后报告位置或将位置保存在变量中以对其进行处理

标签: matlab user-interface matlab-guide axes imshow


【解决方案1】:

您的 GUI 组织似乎有点混乱。您必须小心在 GUIDE 生成的.m 文件中放置额外代码的位置。

我建议你通过调用你自己的初始化函数来替换你放在函数中间的所有代码:

myGuiInitialization(handles) ;

然后,在同一个文件中(在底部),添加以下代码:

function myGuiInitialization(handles)
% path = uigetimagefile;       %// uncomment that to retrieve your intital functionality
% img = imread(path);          %// uncomment that to retrieve your intital functionality
img = imread('peppers.png') ;  %// Delete that to retrieve your intital functionality

himg = imshow(img, 'Parent',handles.axes1) ;    %// get the handle of the image graphic object (needed to attach the datatip to it)

dcm_obj  = datacursormode(handles.figure1);     %// get the handle of the datacursor object
set(dcm_obj,'UpdateFcn', @myDatatipUpdateFcn ); %// assign it a callback
dcm_obj.createDatatip(himg) ;                   %// create a new DATATIP and attach it to the image object

这将加载图像,创建数据提示并将回调关联到它。

管理数据提示更新的单独函数变为:

function txt = myDatatipUpdateFcn(hobj, event_obj)

   handles = guidata( event_obj.Target ) ;  %// retrieve the collection of GUI graphic object handles
   pointPosition = event_obj.Position;      %// get the position of the datatip

   %// Store the position in the figure "appdata", named "pointPosition"
   setappdata( handles.figure1 , 'pointPosition' , pointPosition )  

   %// now we've saved the position in the appdata, you can also display it
   %// on the datatip itself
   txt = {'Point to Compute: ';...
         ['X:',num2str(pointPosition(1))]; ...
         ['Y:',num2str(pointPosition(2))] } ;

现在每次移动数据提示时,最后一个点的位置都会保存在图中AppData中。我建议您使用函数setappdatagetappdata 而不是旧的userdata(这是过去的事情,如果可以的话,请避免使用)。

特别是,不推荐分配对象0userdata,因为这将存储在主根对象中。如果您关闭您的 gui,这些数据可能仍然存在。 setappdata 将存储附加到您的 GUI 的数据,这是一种更简洁的方式(当您的 gui 关闭时,数据会随着您的 gui 消失)。

现在,当您想对保存在 AppData 中的点位置进行操作时,您可以使用 getappdata 检索它们。

出于本示例的目的,我在图上创建了一个按钮。按下时,它将检索点位置并将其显示在文本框中。

当您创建按钮时,GUIDE 将在 gui 主函数中为其创建一个空回调。将以下代码放入其中:

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)

%// retrieve the point position saved in the figure appdata named "pointPosition"
pointPosition = getappdata( handles.figure1 , 'pointPosition' ) ;
%// that's it, you have your point position, now do whatever you need with it

%// prepare a test string with the position and display it in the label
txtstr = sprintf('Point to compute: X=%d , Y=%d',pointPosition) ;
set(handles.txt1,'String',txtstr);

这段代码会产生类似的东西:

数据提示标签会在每次移动时更新位置,但底部的文本标签只有在您按下按钮时才会更新。

【讨论】:

    猜你喜欢
    • 2013-06-18
    • 2014-07-05
    • 2015-07-31
    • 1970-01-01
    • 2017-08-24
    • 2013-08-27
    • 1970-01-01
    • 2017-09-27
    • 2015-07-16
    相关资源
    最近更新 更多