I don't know why hold on wont work- it only produces one graph with one model and not both on one graph

80 ビュー (過去 30 日間)
Amber
Amber 2019 年 12 月 18 日
回答済み: Guillaume 2019 年 12 月 18 日
clear
clear global
%load up a model timestep
load('SIGNUM_6test__Time_180002.mat')
figure
hold on
SIGNUM_drawTIN_g(5e4)
[indices] = SIGNUM_extract_river(240,1);
[along_channel_dist, channel_elev] = SIGNUM_along_channel_dist(indices);
figure
plot(along_channel_dist, channel_elev,'.-')
xlabel('Distance, m')
ylabel('Elevation, m')
load('SIGNUM_6test__Time_40000.mat')
plot(along_channel_dist, channel_elev,'.-')
xlabel('Distance, m')
ylabel('Elevation, m')

回答 (2 件)

Allen
Allen 2019 年 12 月 18 日
Amber, it looks like you are creating a figure twice and only applying hold on to the first, while plotting to the second. Try placing the hold on command just before you use the plot function. Additionally, to help avoid potentially issues that can arise due to the new figure not having an axes currently in the frame, create a blank axes in the figure just before the hold on command.
...
figure % Create a new figure.
axes % Create a blank axes in the current figure (gcf)
hold on % Turn on the 'add next plot' behaviour for the current axes (gca)
plot( ... ) % Plot as normal
...
Additionally, for improved control in your code when using graphics objects, you can assign object handles to variables and then specify which object you want to perform the function on.
hFig = figure; % Create figure and assign handle to 'hFig' variable
% Additional code can be placed here including the creation of other figures
hAx = axes(hFig); % Create a blank axes in the target figure and assign handle to 'hAx'
hold(hAx,'on'); % Turn on the 'add next plot' behaviour to the target axes
plot(hAx, ...) % Specify the target axes prior to normal input arguments in the plot function

Guillaume
Guillaume 2019 年 12 月 18 日
Your code creates a figure and sets the axis to retain plots in this figure with hold on.
Your code then creates a second figure for which by default hold is off. So indeed it will draw one plot and erase it to draw the next one.
After your second figure, issue another hold on. hold is per figure (actually per axis, but your figures have only one axis).

カテゴリ

Help Center および File ExchangeSpecifying Target for Graphics Output についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by