Plot function into subplots
41 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Daniel Schmidt
el 12 de Ag. de 2019
Comentada: dpb
el 13 de Ag. de 2019
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 comentarios
Respuesta aceptada
dpb
el 12 de Ag. de 2019
Editada: dpb
el 12 de Ag. de 2019
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 comentarios
dpb
el 13 de Ag. de 2019
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.
Más respuestas (1)
KALYAN ACHARJYA
el 12 de Ag. de 2019
Editada: KALYAN ACHARJYA
el 12 de Ag. de 2019
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:
Ver también
Categorías
Más información sobre Subplots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!