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

35 次查看(过去 30 天)
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