In some application, there is a need to save the output figure file to do farther operations like data reading, data analysis from an output file, to reference the output figure file. For save a figure to a specific file we use a saveas statement. SAVEAS is an inbuilt function available on matlab for save the figure to specific file format. We can save the figures in different formats like png, jpg,epsc, etc.
Syntax:
The syntax for Matlab saveas functions as shown below:-
saveas(fig,filename)
saveas(fig,filename,formattype)
How to Do Matlab Saveas?
The saveas function is used to save an image with a specific file format, format like .png, .jpg, and many more. So for this function, we have syntax as we saw above. For using a saveas function first we want to create an image for creating an image we use a bar function, bar is an inbuilt function available on matlab, the bar graph creates using bar(x1) function with one bar for each element in x1. If x1 is a y-by-z matrix, then bar creates y groups of z bars. Or we can create an image using a plot function, plot is also an inbuilt function available on matlab plot is used to draw a continuous line and figure like an analog signals. Then this figure is saved using a saveas function. For saving this created image we take saveas in parenthesis gcf, then we take a file name with a figure extension let us assume we take ‘Barchart11’ as a name with extension .png and we also specify the format type in ‘formattype’ argument.
Examples of Matlab Saveas
Following are the examples are given below:
Example 1:
Let us see an example for a saveas statement; basically, saveas function is an inbuilt function available on a matlab, it used for save figure to a specific format. In this example, we create a bar chart, for creating a bar chart we take data in variable x1. We take a bar chart data in a square bracket each data number is separated by a space and these data are assigning to a variable x1. Then we use a bar function which is available on matlab, it’s an inbuilt function of matlab. The bar graph creates using the bar(x1) function with one bar for each element in x1. If x1 is a y-by-z matrix, then bar creates y groups of z bars. Then this generated bar graph is saving in .png format using a saveas function. We take saveas function in the parenthesis we take gcf, then we take a file name with a figure extension this example we take ‘Barchart11’ as a name with extension .png. We save a figure with .png format.
Code:
clc ;
clear all;
close all;
x1 = [5 8 6 7 9 1 0.5 6 7 6 2 4 3 11 1 2];
bar(x1);
saveas (gcf,'Barchart11.png')
Output:

Example 2:
Let us consider an example for saveas function, in this example, we create a figure for creating a figure we use a plot, the plot is an inbuilt function available on matlab for creating a continues signal of a provided data. We take two variables x and y for plotting data. In x variable, we specify a range 1 to 20 with a difference of 1. In the y variable, we take data that we plot on the x axis. Then we use a plot(x, y), it will generate a figure this figure we want to save using a saveas function. We save this figure as an EPS file and we specify epsc to save this image it in colour format. We take saveas function in the parenthesis we take gcf, to save a current figure we must specify fig as gcf, then we take a file name this example we take ‘Barchart1’ as a name and we specify the format type ‘formattype’ to epsc. We save a figure in the EPS file.
Code:
clc;
clear all;
close all;
x = 1:1:20;y = [5 3 8 7 5 15 2 6 7 9 20 1 8 9 6 12.5 4 2 3 9];plot (x,y);
saveas (gcf, 'Barchart1', 'epsc')
Output:

Example 3:
Let us see another example of saveas statement, In this, we create a bar chart and for a bar chart, we take data in variable y1. Now we can take positive and negative integer value of data and plot the bar graph. For plotting the bar graph we use the bar function, which is available on Matlab. Then the generated bar is save in jpg format using saveas statement. We save a figure in the jpg file.
Code:
clc;
clear all;
close all;
y1 = [-5 8 -6 7 -6 18 0.59 6.0 7 -6 2 4 -3 11 -1 2];
bar (y1);
saveas (gcf, 'Bargraph.jpg')
Output:
