Adding multiple function plots to a single figure with subplots - MATLAB

조회 수: 44 (최근 30일)
Mahreen Kohkar
Mahreen Kohkar 2025년 12월 29일 18:43
편집: Matt J 2025년 12월 29일 19:52
I've been trying to run a function three times that plots 2 sub-plots (6 plots total) onto on singular figure.
So I've got a function that reads in a dataset and a manipulator, then manipulates it in 2 different ways, then plots in 2 subplots - the function works as it should, I just can't seem to merge the plots when I'm running it in a seperate script.
Simplified function below:
function function_plot(dataset,manipulator)
%Manipulates Data
figure;
subplot(1,2,2);
imagesc(data_manipulated1);
subplot(1,2,1);
imagesc(data_manipulated2);
end
Simplified script below:
function_plot('data.mat',manipulator1);
function_plot('data.mat',manipulator2);
function_plot('data.mat',manipulator3);
I've also tried the below - but this gives the error "Too many output arguments"
function function_plot(dataset,manipulator,myfigure)
%Manipulates Data
if nargin<4
myfigure = figure;
else
figure(myfigure);
end
subplot(1,2,2);
imagesc(data_manipulated1);
subplot(1,2,1);
imagesc(data_manipulated2);
end
Simplified script:
myfigure = function_plot('data.mat',manipulator1);
function_plot('data.mat',manipulator2,myfigure);
function_plot('data.mat',manipulator3,myfigure);
  댓글 수: 1
Paul
Paul 2025년 12월 29일 18:55
To be clear, you want three outputs of imagesc overlaid on one subplot and three outputs of imagesc overlaid on the other subplot?

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

답변 (2개)

Star Strider
Star Strider 2025년 12월 29일 18:58
I am not certain what you intend by 'merge the plots'.
Note that you need to load a .mat file to use its contents. Consider loading into a variable, creating a structure that you can extract data from in its fields.
Also, consider using the hold function, if appropriate.

Matt J
Matt J 2025년 12월 29일 19:36
편집: Matt J 2025년 12월 29일 19:52
This might be what you want:
Manipulators={manipulator1,manipulator2,manipulator3};
m=3;n=2; %tiling dimensions
%create handles
figure;
ax=gobjects(n,m);
for i=1:m*n;
ax(i)=subplot(m,n,i);
end
ax=ax';
%populate the axes
for j=1:3
function_plot(ax(j,:),dataset,Manipulators{j});
end
function function_plot(ax,dataset,manipulator)
imagesc(ax(1), data_manipulated2);
imagesc(ax(2), data_manipulated1);
end

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by