Maltab: copy axes in a gui to a figure - matlab

I am going to save an image in the GUI to a sperate figure. Some of codes relevant are as follows:
axes(handles.axes1); %axes object
subplot(131); imshow(tempData(:,:,1),[]); title('I1');
subplot(132); imshow(tempData(:,:,2),[]); title('I2');
subplot(133); imshow(tempData(:,:,3),[]); title('I3');
%The three images are displayed in the GUI
% saved to a new figure
handles.axes1
figurenew = figure;
copyobj(handles.axes1,figurenew);
Then, there is an error when running the code:
Error using copyobj
Copyobj cannot create a copy of an invalid handle.
Does that means the handle handle.axes1 no longer exist? Then how to modify the codes to save the displayed image in the GUI?

Each subplot has its own Axes object.
You can get this Axes object by writing as follow.
figure;
axes1 = subplot(131);
Then you can copy the object as you wrote.
figurenew = figure;
copyobj( axes1, figurenew );

Related

How do I copy the legend of the corresponding plot as well as the plot himself into another plot?

I'm working with the following Code to copy a saved plot into another.
fig1 = openfig('ABC.fig');
fig2 = openfig('DEF.fig', 'invisible');
copyobj(fig2.Children.Children, fig1.Children);
This works fine, as long as I'm not using a legend in fig2, then I get a Error message for using too many input arguments in copyobj. In the documentation I learnt, that copyobj doesn't copy the context menu of legends, so I'm asking for your help in copying the legend as well.
As shown here:
plot(rand(2))
l = legend('show'); % legend
ax = gca; % associated axes
fnew = figure;
copyobj([l,ax],fnew)
% This is what the error was telling you.
% You need to pass your axes object
% along with you legend object as an array of graphic objects
[l,ax]
ans =
1×2 graphics array:
Graphics Graphics

retrieving saved data from a boxplot figure in matlab

I have a boxplot that is saved as a figure in Matlab. Unfortunately, I do not have the original data. How do I retrieve the data using code or the user interface?
You may be more precise on your question but here is an example with a simple plot. It shoule be the same with boxplot. It is all about getting axes children.
open myfig.fig; % open your fig file
h = gca; % get the handle of the current axes
hPlot = h.Children; % The children of the axes should be the plot line
XData = hPlot.XData;
YData = hPlot.YData;

Animation of figure with subplots using VideoWriter in MATLAB

I'm trying to create an animation file in MATLAB from a figure with 2 subplots using the VideoWriter. However, the avi file that I get includes only one of the subplots.
Here is the code:
clc
clear
vidObj = VideoWriter('randdata');
open(vidObj)
figure (1)
for i = 1:100
clf
subplot(1,2,1)
imagesc(rand(100))
subplot(1,2,2)
imagesc(rand(100))
drawnow
CF = getframe;
writeVideo(vidObj,CF);
end
There must be something simple going wrong here but I don't know what. I would like to capture the entire figure.
The documentation for getframe states in the first line:
F = getframe captures the current axes
So it's capturing the axes, not figure.
You want to use it as also specified in the documentation
F = getframe(fig) captures the figure identified by fig. Specify a figure if you want to capture the entire interior of the figure window, including the axes title, labels, and tick marks. The captured movie frame does not include the figure menu and tool bars.
So your code should be
clc; clear
vidObj = VideoWriter('randdata');
open(vidObj);
figure(1);
for ii = 1:100
clf
subplot(1,2,1)
imagesc(rand(100))
subplot(1,2,2)
imagesc(rand(100))
drawnow;
% The gcf is key. You could have also used 'f = figure' earlier, and getframe(f)
CF = getframe(gcf);
writeVideo(vidObj,CF);
end
For ease, you might want to just get a File Exchange function like the popular (and simple) gif to create an animated gif.

plot is created in wrong gui window

I have a situation where I create a waitbar, read a large image and plot the image to the gui axes. However what happens is that the plot is created inside the waitbar instead.
hWait = waitbar(0,'1','Name','Reading calibration file ...');
% why do I need the '1' in waitbar ???
readCalibrationImage( handles );
% delete( hWait );
set(handles.figure1,'CurrentAxes',handles.axesROI)
plotROIImage( handles, imagedata, roi, lineV, lineH, doExportPlot )
only if I delete the handle the plot is created in the correct window.
How do I enfore plotting to the correct window?
Try putting a
set(0, 'CurrentFigure', handles.figure1);
in front of the CurrentAxes statement. AFAIK, set(...,'CurrentAxes') does not automatically switch the active figure...

How can I move several existing plots to one subplot in MATLAB?

I have a function, myFunkyFigure, that takes in data, does some funky things, and returns an axis object for the figure it produces.
I would like to be able to invoke this function twice, creating two different figures:
fig(1) = myFunkyFigure(dataSet1);
fig(2) = myFunkyFigure(dataSet2);
Then I would like to put them into a subplot together.
Note that, because of the funkiness of myFunkyFigure, the following does not work.
subplot(2,1,1);
myFunkyFigure(dataSet1);
subplot(2,1,2);
myFunkyFigure(dataSet2);
I believe that I need something along the lines of copyobj, but I haven't been able to get that to work (I tried following a solution in Stack Overflow question Producing subplots and then combine them into a figure later in MATLAB but to no avail).
Obviously, we don't know how "funky" your figures are, but it should be noted in such a case that the cleanest solution would be to modify the function myFunkyFigure such that it accepts additional optional arguments, specifically the handle of an axes in which to place the plot it creates. Then you would use it like so:
hSub1 = subplot(2,1,1); %# Create a subplot
myFunkyFigure(dataSet1,hSub1); %# Add a funky plot to the subplot axes
hSub2 = subplot(2,1,2); %# Create a second subplot
myFunkyFigure(dataSet2,hSub2); %# Add a funky plot to the second subplot axes
The default behavior of myFunkyFigure (i.e. no additional arguments specified) would be to create its own figure and place the plot there.
However, to answer the question you asked, here's a way to accomplish this given that you are outputting the axes handles (not the figure handles) in the vector fig (note: this is basically the same solution as the one given in the other question, but since you mention having trouble adapting it I thought I'd reformat it to better fit your specific situation):
hFigure = figure(); %# Create a new figure
hTemp = subplot(2,1,1,'Parent',hFigure); %# Create a temporary subplot
newPos = get(hTemp,'Position'); %# Get its position
delete(hTemp); %# Delete the subplot
set(fig(1),'Parent',hFigure,'Position',newPos); %# Move axes to the new figure
%# and modify its position
hTemp = subplot(2,1,2,'Parent',hFigure); %# Make a new temporary subplot
%# ...repeat the above for fig(2)
The above will actually move the axes from the old figure to the new figure. If you want the axes object to appear in both figures, you can instead use the function COPYOBJ like so:
hNew = copyobj(fig(1),hFigure); %# Copy fig(1) to hFigure, making a new handle
set(hNew,'Position',newPos); %# Modify its position
Also note that SUBPLOT is only used here to generate a position for the tiling of the axes. You could avoid the need to create and then delete subplots by specifying the positions yourself.
The code from gnovice didn't work for me.
It seemed that a figure couldn't be made the child of another figure. For example hNew = copyobj(fig(1),hFigure); gave the error
Error using copyobj
Object figure[1] can not be a child of parent
figure[1]
Instead, I had to make the axes children of the new figure. This is the function I came up with
function []= move_to_subplots(ax,a,b)
% %
% Inputs:
% inputname:
% Outputs:
% name: description type units
% saved data: (does this overwrite a statically named file?)
% plots:
%
% Standard call:
%
%
% Written by C. Hogg Date 2012_06_01
%
%
debugmode=0;
hFigure=figure();
if ~exist('a')
a=ceil(sqrt(length(ax)));
end
if ~exist('b')
b=1;
end
if a*b<length(ax)|~exist('a')|~exist('b')
disp('Auto subplot sizing')
b=ceil(length(ax)/a);
end
for i=1:length(ax)
hTemp = subplot(a,b,i,'Parent',hFigure); %# Make a new temporary subplot
newPos = get(hTemp,'Position'); %# Get its position
delete(hTemp);
hNew = copyobj(ax(i),hFigure);
set(hNew,'Position',newPos)
end
%% Debug. Break point here.
if debugmode==1; dbstop tic; tic; dbclear all;end
end
This seems to work for me.