How to open an axes object inside a GUI ?

8 次查看(过去 30 天)
I am using GUIDE and plot a graph in axes object created within the GUI. I would like to have a way where I can open that axes in a figure window outside the GUI. (best if I can just double click to open !!) Is there any such way to do so ?

采纳的回答

Evan
Evan 2013-7-29
编辑:Evan 2013-7-29
If you're asking if this sort of functionality is available without doing your own coding when interacting with axes in figure windows, I do not believe it is.
One option you could look into is always plotting the data in a separate figure window and being able to dock or undock that "sub-figure" in your main figure. I think that would require extensive work, though, and personally I don't think it would be as slick or ascetically pleasing as the double-click idea that you mentioned.
Another, probably more appealing solution: If you save all the plotted data in the handles structure during your plotting function, you could just run the same plotting function but output to a figure on the activation of a pushbutton or mouse click. If you want this to happen when the user clicks within your axes, look into the WindowButtonDownFcn. If you want to require a double-click for the figure to open, look into using a timer object withing the button down function as discussed here.
  3 个评论
Evan
Evan 2013-7-29
编辑:Evan 2013-7-29
Since you're new to Matlab and GUIs, it might be easiest to first set up functionality for the user to open up the graph in a new window using a pushbutton. This way, you can make sure you have the basic operations down without having to dig too deep into the many functions available to your figure window.
Later, if you want to learn how modify the functionality so that the window is opened on a double-click, you can do so without having too scrap much of your old code.
Here's a simple example for plotting your data in a separate figure window using a pushbutton:
function myCalculations_Callbacks(hObject,eventdata,handles)
% Here is an example callback that you would find in your GUI's mfile.
% This would correspond to whatever routine generates the data in your graph.
% We'll make some random data, then save it to the handles structure.
% This allows us to use it later in another function.
x = [0:pi/16:2*pi];
y = sin(x);
plotRoutine(x,y,handles.myAxes) %send data to our custom plotting routine
myTitle = 'Sin(x)';
myXLabel = 'x';
myYLabel = 'y';
title(myTitle);
xlabel(myXLabel);
ylabel(myYLabel);
handles.x = x; %put all the data you want to keep in handles
handles.y = y;
handles.myTitle = myTitle;
handles.myXLabel = myXLabel;
handles.myYLabel = myYLabel;
guidata(hObject,handles)
Next, you'll need to add code to the callback for your pushbutton so that it will make an identical plot in a new figure window:
function myPushbutton_Callback(hObject,handles,eventdata)
plotRoutine(handles.x,handles.y)
title(handles.myTitle);
xlabel(handles.myXLabel);
ylabel(handles.myYLabel);
Finally, since both functions call the same plotRoutine, you'll need to create it in your mfile. To dinstinguish between plotting to an embedded axes and one in a new figure window, you could base it on how many arguments are passed in. if x,y data and a handles to an axes object is passed in, the function will assume its in your main GUI. if only x,y data is passed in, it'll plot to a new figure window:
function plotRoutine(varargin)
x = varargin{1};
y = varargin{2};
if narargin > 2
ax = varargin{3};
plot(ax,x,y)
else
figure;
plot(gca,x,y)
end
pr
pr 2013-12-7
@ Evan Excellent!!! You forgot to include ''narargin = length(varargin);'' in the function ''plotRouting''. and thankyou for the solution.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by