Function that creates figures only?

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)

 Accepted Answer

function fig = myfunc()
Z = peaks(100) ;
fig(1) = figure ;
pcolor(Z) ; shading interp ;
fig(2) = figure ;
surf(Z) ; shading interp ;

4 Comments

@KSSV, thanks for your response. I was thinking something like this. Is the approach correct? The values and everything is just arbitrary.
clear
clc
x=[2 3 4]
y=[3 4 5]
[p,q]=plot_out(x,y)
function [p,q]=plot_out(x,y)
figure('name','p')
p=plot(x,y)
figure('name','q')
q=plot(x,x)
end
I was looking at your script again and it is very useful but the function you declare doesnt have any input. Can you pelase explain how this works?
I would like to have numerical inputs.
Also from what i see the structure of your function is very useful as if you want to add more figures you just add fig(3)=... etc

No, that proposed code creates figures as well as plotting, but the requirements of the question do not permit anything other than plotting to be done.

@Walter Roberson thanks a lot for your help, i rephrased my question so that it is accurate.

Sign in to comment.

More Answers (2)

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 Comment

@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.

Sign in to comment.

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 Comments

@Awais Saeed Thanks a lot!!!
And could i make this function creating multiple plots??
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.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

on 4 Aug 2021

Commented:

on 4 Aug 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!