Function that creates figures only?

78 visualizzazioni (ultimi 30 giorni)
Dimitris K
Dimitris K il 4 Ago 2021
Commentato: Dimitris K il 4 Ago 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)

Risposta accettata

KSSV
KSSV il 4 Ago 2021
function fig = myfunc()
Z = peaks(100) ;
fig(1) = figure ;
pcolor(Z) ; shading interp ;
fig(2) = figure ;
surf(Z) ; shading interp ;
  4 Commenti
Walter Roberson
Walter Roberson il 4 Ago 2021

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 il 4 Ago 2021
@Walter Roberson thanks a lot for your help, i rephrased my question so that it is accurate.

Accedi per commentare.

Più risposte (2)

Walter Roberson
Walter Roberson il 4 Ago 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 Commento
Dimitris K
Dimitris K il 4 Ago 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.

Accedi per commentare.


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

Accedi per commentare.

Categorie

Scopri di più su Graphics Object Programming in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by