Function that creates figures only?

78 次查看(过去 30 天)
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
KSSV 2021-8-4
function fig = myfunc()
Z = peaks(100) ;
fig(1) = figure ;
pcolor(Z) ; shading interp ;
fig(2) = figure ;
surf(Z) ; shading interp ;
  4 个评论
Walter Roberson
Walter Roberson 2021-8-4

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.

Dimitris K
Dimitris K 2021-8-4
@Walter Roberson thanks a lot for your help, i rephrased my question so that it is accurate.

请先登录,再进行评论。

更多回答(2 个)

Walter Roberson
Walter Roberson 2021-8-4

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 个评论
Dimitris K
Dimitris K 2021-8-4
@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
Awais Saeed 2021-8-4
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 个评论
Dimitris K
Dimitris K 2021-8-4
@Awais Saeed Thanks a lot!!!
And could i make this function creating multiple plots??
Awais Saeed
Awais Saeed 2021-8-4
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by