Plot function into subplots
44 ビュー (過去 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 で Subplots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!