Plot function into subplots

조회 수: 42 (최근 30일)
Daniel Schmidt
Daniel Schmidt 2019년 8월 12일
댓글: dpb 2019년 8월 13일
Hey,
I have a function which creates a plot. Now I want to run this function multiple times and put each plot into a subplot slot.
It should be something like this:
function[] = myplot(x,y)
plot(x,y);
end
subplot(3,1,1) = myplot(input_x1,input_y1);
subplot(3,1,2) = myplot(input_x2,input_y2);
subplot(3,1,3) = myplot(input_x3,input_y3);
This doesn't seem to work, is there an easy way to do this? Maybe with an appropriate output for the function which can be used?
Or maybe with the correct plot handle?
Any help for a simple solution would be nice.
Thanks.

채택된 답변

dpb
dpb 2019년 8월 12일
편집: dpb 2019년 8월 12일
for i=1:3
hAx(i)=subplot(3,1,i);
plot(x(:,i),y(:,i));
end
presuming all x,y are same length. If they're not, then saving them as cell arrrays is an expedient way; just dereference the array with {} inside the loop
  댓글 수: 4
Daniel Schmidt
Daniel Schmidt 2019년 8월 13일
편집: dpb 2019년 8월 13일
Well, in my plot function I do not immediately call the plot function it is more like this:
function[] = myplot(x,y)
*getdata;
*arrange data into multiple sets (y_1...y_n)
*manipulate and change datasets
plot(x,y_1)
hold on;
plot(x,y_2)
[...]
plot(x,y_n)
end
for i=1:n
hAx(i)=subplot(3,1,i);
myplot(x(:,i),y(:,i));
end
So it plots at the end of the function.
dpb
dpb 2019년 8월 13일
Well, your function plots everything on one axes by explicitly calling plot() multiple times in a row without either creating a subplot or having access to the handles of the subplot axes already created -- so how could it possibly do anything but?
Using multiple variables named sequentially as done is a sure sign the data are not stored effectively -- makes the need to write the same code multiple times as you have inside that function and also makes it very difficult to manipulate different pieces of the data.
You really should recast your routine so that it only retrieves and plots the dataset requested each time it is called but it needs the loop index and subplot handle to do so.
In reality, this is a case where that code logic should be inside the function itself or the logic to retrieve should be inline inside the loop--the functional factorization is at the wrong level and by not having the overall x,y data in 2D X,Y arrays or cell arrays completely prevents having any effective way to address the portions of it that you want.
As long as your "*arrange data into multiple sets" returns the named variables with the metadata of which set in the variable name and as different variables you're stuck.

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

추가 답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 8월 12일
편집: KALYAN ACHARJYA 2019년 8월 12일
Is there an easy way to do this?
function myplot(x,y)
plot(x,y);
end
Main Script: Examples. Considering all x & y have different functions and ranges
% Define all x y data
% Thease are just examples
input_x1=[1:10];
input_y1=exp(input_x1);
input_x2=[11:20];
input_y2=log(input_x2)
input_x3=[31:45];
input_y3=exp(-input_x3);
%% Plots
subplot(3,1,1),myplot(input_x1,input_y1);
subplot(3,1,2),myplot(input_x2,input_y2);
subplot(3,1,3),myplot(input_x3,input_y3);
Result:
  댓글 수: 1
Daniel Schmidt
Daniel Schmidt 2019년 8월 12일
This doesn't seem to work for me.
If if use the default plot(x,y) function it works.
Put with my own plot function it doesn't.
My own plot function is more complex than the simple example above.

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

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by