# Function that creates figures only?

3 views (last 30 days)
Dimitris K on 4 Aug 2021
Commented: Dimitris K on 4 Aug 2021
Hello everyone,
I was wondering if i could create a function that only produces figures (plots). And if yes could someone give me a small example??
Lastly, what would be the output of that function??
eg [plot1,plot2,plot3]=plot_fun(input1,input2,etc)

KSSV on 4 Aug 2021
function fig = myfunc()
Z = peaks(100) ;
fig(1) = figure ;
pcolor(Z) ; shading interp ;
fig(2) = figure ;
surf(Z) ; shading interp ;
Dimitris K on 4 Aug 2021
@Walter Roberson thanks a lot for your help, i rephrased my question so that it is accurate.

### More Answers (2)

Walter Roberson on 4 Aug 2021

No, it is not possible in MATLAB to create a function that only produces plots without doing anything else.

In the case where there is no current axes then the function would have the side effect of also creating an axes (and possibly a figure too.)

In the case where there is an existing axes that is set to replace children, then the function would have the side effect of clearing the axes, unless the function also took the step of setting the axes to retain plots. However, setting the axes to retain plots would involve doing something additional to "just" producing plots.

If the axes is set to retain children, then plotting can have side effects on axes limits and ticks and potentially creating additional legend entries, and changing the index of the "current" color number.

It is difficult to create plots without any side effects unless you specifically control for the side effects, but that controlling is doing more than "just" plotting.

Sometimes my housemate hands me something and asks me to "just hold this". I can never do what they are asking: every single time I have to breathe as well, and breathing is not just holding the object.

##### 1 CommentShowHide None
Dimitris K on 4 Aug 2021
@Walter Roberson Thanks a lot for your response Walter! I completely understand what you mean... You are right. I think the way i sentenced my question was not crystal clear. What i am interested in is creating a function that creates figures no matter the side effect.
So yes as long as you can hold what your housemate wants without breaking it, there is no problem with the breathing part too.

Awais Saeed on 4 Aug 2021
If you only want to create plots then you can simply do it as following
clc
x = 0:pi/100:2*pi;
y = sin(x);
create_plots(x,y)
function [output] = create_plots(x,y)
plot(x,y)
output = 99;
end
Since you might not need the OUTPUT of the function creating plots, you can set it to any random value.
##### 2 CommentsShowHide 1 older comment
Awais Saeed on 4 Aug 2021
You are welcome. I do not think I understand your question completely. The function is just plotting the input data. You can call the fucntion again if you want to create a new plot somewhere in your code again.