Plot function into subplots
조회 수: 42 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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
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
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:
참고 항목
카테고리
Help Center 및 File Exchange에서 Axis Labels에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!