Aeroplane
Aeroplane

Reputation: 169

How to save plot with its colourbar as .fig in Matlab GUI

I have a button which will produce plot along with its color bar by clicking on it. The button relates to certain function called zzpcolor. Inside zzpcolor, I use pcolor syntax to produce shake map.

Inside the callback function, I use hold on to hold the figure generated by zzpcolor. Then I add another plot to the same axis. This is part of the script within the push botton callback.

axes(handles.axes1);
axes1.Position=[0.1300 0.1100 0.7750 0.8150];
[X,Y,Z]=plotpcolor(fnamedat);
hold on
zzpcolor(X,Y,Z);
shading flat
LimitPlot
hold on
plot_google_map
hold on
scatter(datageo(:,1),datageo(:,2),'MarkerFaceColor',[1 0 0])
hold off

The syntax worked just fine. I use this syntax to save the plot as jpg in another callback function.

newfig1 = figure('Visible','off'); 
copyobj(handles.axes1, newfig1);
[filename,pathname]= uiputfile('*.jpg','Save as'); 
hold on
wmmicolorbarsetting;
saveas(newfig1,[pathname,filename],'jpg');

it works just fine. But when I try to save it as .fig using similar syntax like this,

newfig1 = figure('Visible','off'); 
copyobj(handles.axes1, newfig1);
[filename,pathname]= uiputfile('*.fig','Save as'); 
hold on
wmmicolorbarsetting;
saveas(newfig1,[pathname,filename],'fig');

the .fig file contains nothing. Why?

Upvotes: 0

Views: 127

Answers (1)

Suever
Suever

Reputation: 65430

The .fig file does contain something. You set the figure Visible property to 'off' so the figure doesn't actually show up when you create the figure or when you load the figure from the file.

You can verify this by loading the .fig file with hgload and setting the Visible property to 'on'.

fig = hgload([pathname, filename]);
set(fig, 'Visible', 'on')

You can also look at the produced .fig file and ensure that it is non-empty.

You can fix this by setting Visible to 'on' prior to saving.

A note on figure visibility: Setting Visible to 'off' is useful for saving a figure as a format other than .fig (png, jpeg, etc.) because you can create an image without worrying about a bunch of figures showing up as your script runs. In these cases, no user interaction is required. As you've found out, if you actually need to see/interact with figures, Visible should be 'on' to be useful.

Upvotes: 1

Related Questions