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

26 views (last 30 days)
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 Comment
Paul
Paul 4 minuten ago
To be clear, you want three outputs of imagesc overlaid on one subplot and three outputs of imagesc overlaid on the other subplot?

Sign in to comment.

Answers (2)

Star Strider
Star Strider minder dan een minuut ago
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 ongeveer 4 uur ago
Edited: Matt J ongeveer 4 uur ago
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

Tags

Community Treasure Hunt

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

Start Hunting!