Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

GUI save axes as fig / picture file

3,513 views
Skip to first unread message

Markus

unread,
Aug 11, 2010, 1:44:24 PM8/11/10
to
I have a matlab gui running, that outputs a FFT plot on an axes. I want to save this plot for future reference and tried using this following code. How ever it gives me the following error:

??? Error using ==> copyobj
Conversion to double from struct is not possible.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function uipushtool3_ClickedCallback(hObject, eventdata, handles)
% hObject handle to uipushtool3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

plotH = get(handles.axes2);
orignalAxes = gca;
% uicontrol('parent',gcf)


%Create a ne figure
newFig = figure(2);
%Create a copy of the axes
newA = copyobj(orignalAxes,newFig);
%copy the plot across
newPlotH = copyobj(plotH,newA);

%Print the fig with the print function
print(newFig,'-dpng','image.png');
%close the new figure
close(newPlotH)
guidata(hObject,handles);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Does anyone have an idea to get around this error or help me saving my plot? Any help will be appreciated.

Thanks, Markus

Martin V.

unread,
Feb 27, 2011, 5:50:06 AM2/27/11
to
You can try this code

F=getframe(handles.axes1); %select axes in GUI
figure(); %new figure
image(F.cdata); %show selected axes in new figure
saveas(gcf, 'path', 'fig'); %save figure
close(gcf); %and close it

Oliver Woodford

unread,
Feb 27, 2011, 9:40:36 AM2/27/11
to
"Markus" wrote:
> I have a matlab gui running, that outputs a FFT plot on an axes. I want to save this [axes] for future reference.

Export_fig allows you to specify the axes in the figure you want to save. E.g.:

export_fig(orignalAxes, 'image.png');

Download export_fig here:
http://www.mathworks.com/matlabcentral/fileexchange/23629-exportfig

Martin V.

unread,
Feb 27, 2011, 9:56:29 AM2/27/11
to
"Oliver Woodford" wrote in message <ikdnp4$2j$1...@fred.mathworks.com>...

I tried to use your code, but exported fig. file didn't have menu bar and toolbar.

Ali

unread,
Apr 8, 2011, 4:45:21 PM4/8/11
to
you can use getimage instead of getframe

Brad Porter

unread,
Apr 25, 2011, 2:02:05 PM4/25/11
to
"Markus" wrote in message <i3unho$9ip$1...@fred.mathworks.com>...
> I have a matlab gui running, that outputs a FFT plot on an axes. I want to save this plot for future reference ...

The simplest thing that I know of is to do the following:
%set(handles.figure1, 'CurrentAxes', handles.axes3);
figure(100)

Not very elegant, but gets the job done. You can then save the figure as a .fig file using the drop down menus and not just as an image. You can also copy the figure using the drop down menu for easy inclusion in presentations.

Brad

plasmageek

unread,
Aug 29, 2017, 11:53:19 AM8/29/17
to
So this is years later of course, but I attempted to do the same thing Markus did with almost identical code. And then spent almost an hour pouring through the forums looking for an answer as to how to make it work....combining various solutions gives:

oldAx=get(handles.axes2,'children');
fig=figure; %new figure
ax=axes; %new axes
copyobj(oldAx,ax) %copies all ur lines to new axes

%%I didn't have any of these on my old axes, so I'm not sure if they're copied to the new one. %%If not you can set it up like normal
grid on
box on
xlabel('Distance (um)')
ylabel('Intensity (A.U.)')
print(fig,['width_' fname '.png'],'-dpng')
close(fig); %and close it

Enjoy!

"Markus" wrote in message <i3unho$9ip$1...@fred.mathworks.com>...
0 new messages