How to move an object from one plot to another inside a GUI using GUIDE

Hi ,
I would like to move an object from one plot to another plot within the same GUI which I have created using GUIDE. Can anyone help me please.
Thanks
Vivekram

5 件のコメント

Jan
Jan 2013 年 3 月 21 日
The question is not clear. Do you want a copy, or an animation, move it with the mouse or programmatically? What is an "object" and what is a "plot" exactly? Please post more details.
Vivekram
Vivekram 2013 年 3 月 21 日
Hi Jan,
I apologies for not being clear. What I wanted to ask was, say I have two plots within my GUI. One of the plots has a circle. However, I would like to move this circle from the present plot to another plot using mouse. That is where I am facing the trouble. I hope I was clear now.
Thanks,
Viv
Sven
Sven 2013 年 3 月 21 日
Vivekram, in what way does my answer not do what you're asking?
In the example there are two plots ( axes is the correct term).
In the example you "use the mouse" (ie, click the button) to move a line from one plot to the other.
Isn't this what you're looking for?
Image Analyst
Image Analyst 2013 年 3 月 21 日
Whatever you did to plot it the first time, why don't you just do it again in the other axes? You can then call cla reset on the first axes if you want.
Vivekram
Vivekram 2013 年 3 月 25 日
Though I apologies for my late reply, I would like to thank Image analyst for your suggestion was really helpful

サインインしてコメントする。

 採用された回答

Sven
Sven 2013 年 3 月 21 日
編集済み: Sven 2013 年 3 月 21 日
Vivekram, you can change any handle graphics "Parent" property. Here's a fully working example:
function F = tmpfunc()
F = figure;
ax1 = subplot(1,2,1); axis([0 10 0 1])
ax2 = subplot(1,2,2); axis([0 10 0 1])
plotH = plot(ax1,rand(10,1),'.r-');
uicontrol(F, 'String','Push Me', 'Position', [0 0 100 30],'Callback',@push_callback)
function push_callback(src,evnt)
currentParent = get(plotH,'Parent');
if currentParent==ax1
newParent=ax2;
else
newParent=ax1;
end
set(plotH,'Parent',newParent)
end
end
See the set() call at the end there? All it takes is the handle of the thing you want to move, and the handle of the thing you want to move it to.
Does this help answer your question?

7 件のコメント

Vivekram
Vivekram 2013 年 3 月 21 日
Thanks Sven,
It does give me a clear concept. However, if I may ask, since I have both these plots inside my GUI with tags axes1 and axes2. How do I then perform the same operation?
Regards,
Viv
Sven
Sven 2013 年 3 月 21 日
I don't quite understand what you mean.
In my example, I have two axes with variable names ax1 and ax2, and one plot handle with variable name plotH. I move the plot handle from one axes to the other.
What do you mean by "plots inside my GUI with tags axes1 and axes2"?
Vivekram
Vivekram 2013 年 3 月 22 日
Hi Sven,
Thanks for your reply. I understand what you are trying to say and thanks for that. I will let you know once I implement it. I will also upload the gui that I am trying to do if I fail in implementing your procedure. Thanks once again
Viv
Vivekram
Vivekram 2013 年 3 月 22 日
Hi Sven,
Please look at the following code that I used to create the GUI. I apologies as it is long. To give you a brief description, I am using the GUIDE function to create the GUI. The image of GUI is similar to that of yours. The following is the code:-
function varargout = try1(varargin)
% TRY1 MATLAB code for try1.fig
% TRY1, by itself, creates a new TRY1 or raises the existing
% singleton*.
%
% H = TRY1 returns the handle to a new TRY1 or the handle to
% the existing singleton*.
%
% TRY1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TRY1.M with the given input arguments.
%
% TRY1('Property','Value',...) creates a new TRY1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before try1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to try1_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 try1
% Last Modified by GUIDE v2.5 22-Mar-2013 09:16:45
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @try1_OpeningFcn, ...
'gui_OutputFcn', @try1_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 try1 is made visible.
function try1_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 try1 (see VARARGIN)
% Choose default command line output for try1
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes try1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = try1_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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes1)
h=filledcircle([0,0],5,1000,'b');
currentParent = get(h,'Parent')
if currentParent==axes1
newp=axes2
else
newp=axes1
end
set(h,'Parent',newp)
% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate axes1
Sven
Sven 2013 年 3 月 22 日
Hi Vivekram, I can see two problems.
Firstly, this code:
axes(handles.axes1)
h=filledcircle([0,0],5,1000,'b');
currentParent = get(h,'Parent')
if currentParent==axes1
newp=axes2
else
newp=axes1
end
set(h,'Parent',newp)
seems to refer separately to handles.axes1 and simply axes1. I think you probably mean handles.axes1 and handles.axes2 (since these axes were created by GUIDE, only you know what tag you gave it).
Next, notice that inside your pushbutton callback you are creating a circle. This means that every time the button is pressed, a new circle is created. Also, immediately before you create it you activate handles.axes1, and immediately after you create it you check the 'Parent' of the circle. It will always be handles.axes1 with your current code.
You should either:
  1. Create the circle outside your pushbutton code (ie, in the initialisation function) and store it's handle in the handles structure. And then only move it from parent to parent inside the pushbutton callback, OR
  2. As ImageAnalyst suggested, don't bother moving it, just make a new circle each time (similar to your current code). The difference is that you need to delete the old circle when you make a new one. Your current code doesn't do this, and would need to be adjusted to make sure you store the handle to the circle somewhere where the next call to the pushbutton callback can access it (ie, in the handles structure, updating guidata() after it has been added)
Vivekram
Vivekram 2013 年 3 月 25 日
Thanks Sven for the help. Will do as you say
Sven
Sven 2013 年 3 月 27 日
No problems Vivekram, please hit accept if your question is answered.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeInteractive Control and Callbacks についてさらに検索

質問済み:

2013 年 3 月 21 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by