MATLAB Answers

Subplot on a saved figure only working for one subplot

7 views (last 30 days)
Fernando on 31 Mar 2021
I have a figure with three subplots (1 column, 3 rows) that has been pre-allocated with all the formatting and saved.
% allocate figures for saving joint angles per subject
pre_ang_fig = figure();
pre_ax1 = subplot(3,1,1);
ylabel('Ankle angle (\circ)')
title('Pre Angles')
pre_ax2 = subplot(3,1,2);
ylabel('Knee angle (\circ)')
pre_ax3 = subplot(3,1,3);
xlabel('Gait cycle %')
ylabel('Hip angle (\circ)')
saveas(pre_ang_fig,[sub_ext '_' trans_typ '_pre_joint_angles.fig'])
In a loop,I then want to load that figure, and plot some additional data on each subplot, maintaining axis formatting and keeping all data present.
My current code (see below), loads the figure fine and plots perfectly on the third subplot but the first two are remade every time. I have tried using axes instead of subplot but I end up with the exact same result.
pre_ang_fig = openfig([sub_ext '_' trans_typ '_pre_joint_angles.fig']); %load figure
if all_cycles(ii,4) == 1 % if right leg plot right leg data in red
hold all
plot(cycle_norm(:,13),'r') %ankle
plot(cycle_norm(:,9),'r') %knee
plot(cycle_norm(:,5),'r') %hip
elseif all_cycles(ii,4) == 2 % if left leg, plot left leg data in green
hold all
plot(cycle_norm(:,15),'g') %ankle
plot(cycle_norm(:,11),'g') %knee
plot(cycle_norm(:,7),'g') %hip
saveas(pre_ang_fig, [sub_ext '_' trans_typ '_pre_joint_angles.fig'])
I would like all subplots to look like the bottom subplot here, but instead the first two get overwritten each time and so this is the resulting figure I end up with.
Any help is really appreciated!

Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 31 Mar 2021
I presume that you have a loop before "if... end" operations, correct?
You should put "hold all" command after your "plot()" command not after "figure" command.

Community Treasure Hunt

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

Start Hunting!

Translated by