I use saveas or savefig programmatically to save a specific figure to a .fig file. However it save all open figures to this file.
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
When I open the fig file that was saved I expect to see only the figure that I saved when the file is opened. What I get is all figures that were open at the time I saved the file. This did not happen in older Matlab versions used when this code was written. I am currently using R2018a.
0 commentaires
Réponses (2)
Mark Sherstan
le 11 Déc 2018
Are you specifying the exact figure you want to save? For example:
h = figure(1)
surf(peaks)
savefig(h,'PeaksFile1.fig')
g = figure(2)
surf(peaks)
savefig(g,'PeaksFile2.fig')
etc...
1 commentaire
Stephen23
le 12 Déc 2018
Frank Galetta's "Answer" moved here:
Here is the callback that performs the savefig. The figure handle is fig_h. I also tried using gcf in place of fig_h and got the same result. I checked fig_h and it looks like the handle to the correct figure and is not an array of figure handles.
% --------------------------------------------------------------------
function file_save_as_Callback(hObject, eventdata, handles)
% hObject handle to file_save_as (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
savePath=pwd; % Get current dir
art_h=guidata(findobj('Tag','trialkeyPopupmenu'));
if exist(art_h.otisPrefs.nbprefs.plotfilesPath,'dir')
dirTest=exist(art_h.otisPrefs.nbprefs.plotfilesPath);
if dirTest==7
cd(art_h.otisPrefs.nbprefs.plotfilesPath);
end
end
[obj_h,fig_h]=gcbo; % Get object and figure handles of current object
[file,path] = uiputfile('*.fig','Save file name'); % Request file name
if ~ischar(file)
return;
else
% Save path to plot file directory in otisPrefs
art_h.otisPrefs.nbprefs.plotfilesPath=path;
% Add plotfilesPath to otisPrefs in ART handles
guidata(findobj('Tag','trialkeyPopupmenu'), art_h);
% Save current otisPrefs structure with figure in UserData
figUser=get(fig_h,'UserData');
figUser.fileNameAndPath=[path file];
set(fig_h,'UserData',figUser);
%Update preferences in figUser with current content of plot labels
%figUser=update_fig_pref_labels(fig_h,figUser);
% Point to selected dir
cd(path);
% Set Name of graph gui and save it as a fig file
dotIndex=strfind(file,'.');
filename=file(1:dotIndex-1);
set(handles.nbFullGui,'Name',filename);
legend_h=findobj(handles.nbFullGui,'Tag','UntitledNbFullnbFullLegend');
set(legend_h,'Tag',[filename 'nbFullLegend']);
savefig(fig_h,file); % Save fig file
cd(savePath); % cd back to original path
%Update graph names in ART
current_graph_names(art_h);
end
%Also save the figure as a PDF if specified
try
if handles.saveFigPdf
handles.pdfFileName=[filename '.pdf'];
file_print_pdf_Callback(hObject, eventdata, handles);
handles.saveFigPdf=0;
guidata(hObject,handles);
end
catch
end
Frank Galletta
le 11 Déc 2018
1 commentaire
Mark Sherstan
le 11 Déc 2018
Can you please post a sample of your code where you are running into the issue?
Voir également
Catégories
En savoir plus sur Printing and Saving dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!