Guardar contenido de un axes en Matlab
Hola Jose,
Exactamente no es lo que quiero pero he hecho una gui de prueba con 4 botones y dos axes.
En los 2 primeros intento copiar el plot del axes principal como formato jpg.
En los otros 2 hago lo mismo pero como formato .fig.
En el código comento lo que va mal y me gustaría encontrar la solución en ambos casos.
Gracias.
Aquí está el código
function varargout = guiPrueba(varargin)
% GUIPRUEBA M-file for guiPrueba.fig
% GUIPRUEBA, by itself, creates a new GUIPRUEBA or raises the existing
% singleton*.
%
% H = GUIPRUEBA returns the handle to a new GUIPRUEBA or the handle to
% the existing singleton*.
%
% GUIPRUEBA('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUIPRUEBA.M with the given input arguments.
%
% GUIPRUEBA('Property','Value',...) creates a new GUIPRUEBA or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before guiPrueba_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to guiPrueba_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 guiPrueba
% Last Modified by GUIDE v2.5 14-Dec-2011 21:09:01
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @guiPrueba_OpeningFcn, ...
'gui_OutputFcn', @guiPrueba_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 guiPrueba is made visible.
function guiPrueba_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for guiPrueba
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes guiPrueba wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = guiPrueba_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;
x=-6*pi:pi/20:6*pi;
axes(handles.axes1);
plot(x,sin(x));
% --- Botón Guardar
function pushbutton1_Callback(hObject, eventdata, handles)
% Guardar imagen
rgb = getimage(handles.axes1);
if isempty(rgb) %Es vacío porque supongo no lo considera una imagen, por lo tanto no llega a guardar el plot como imagen
disp('no hay imagen');
return;
end
% Guardar archivo
formatos = {'*.jpg','JPEG (*.jpg)';'*.tif','TIFF (*.tif)'};
[nomb,ruta] = uiputfile(formatos,'GUARDAR IMAGEN');
if nomb==0, return, end
fName = fullfile(ruta,nomb);
imwrite(rgb,fName);
% --- Botón obtener imagen
function pushbutton2_Callback(hObject, eventdata, handles)
[nomb,ruta] = uigetfile('*.jpg','Elegir IMAGEN');
if isequal(nomb,0) || isequal(ruta,0)
return;
end
a=imread([ruta nomb]);
set(handles.axes2,'Visible','on');
axes(handles.axes2);
imshow(a);
% --- Guarda el plot en una figura.
function pushbutton3_Callback(hObject, eventdata, handles)
%Crea una figura
graficaTiempo = figure;
%No muestro figura
set(graficaTiempo,'Visible','off');
%copia el contenido del axes1 en la figura
copyobj(handles.axes1,graficaTiempo);
%Elige ruta para guardar la figura
formatos = {'*.fig','Fig (*.fig)'};
[nomb,ruta] = uiputfile(formatos,'GUARDA FIGURA');
if isequal(nomb,0) || isequal(ruta,0)
return;
end
rutaCompleta=fullfile(ruta,nomb);
%Guarda la figura en la ruta
saveas(graficaTiempo,rutaCompleta);
% --- Obtiene la figura guardada e intenta mostrarla de nuevo en el axes.
function pushbutton4_Callback(hObject, eventdata, handles)
%Esta es la parte que no me funciona porque no sé como hacerlo pero por
%lógica he supuesto que sería algo así.
%Obtengo ruta de la figura guardada
[nomb,ruta] = uigetfile('*.fig','Elegir figura');
if isequal(nomb,0) || isequal(ruta,0)
return;
end
rutaCompleta=fullfile(ruta,nomb);
%abre figura
f=open(rutaCompleta);
%el error está en este comando que intenta copiar el contenido de la figura
%a un axes
copyobj(f,handles.axes2);