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

27 ビュー (過去 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 2分 前
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 約5時間 前
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 約4時間 前
編集済み: Matt J 約4時間 前
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 Center および File ExchangeAnnotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by