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일

1 개 추천

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

댓글 수: 4

@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
Dimitris K
Dimitris K 2021년 8월 4일
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
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일

0 개 추천

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일

0 개 추천

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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

질문:

2021년 8월 4일

댓글:

2021년 8월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by