How can I solve this problem "Invalid or deleted object." Is hold 'on' missing somewhere?

20 ビュー (過去 30 日間)
Hello everyone,
I really hope someone can help me. I tried to figure out what this Error "Invalid or deleted object." causes but I can't find the solution. Any advice is really much appreciated. I uploaded a Matlab file for better testing purposes.
My goal is basically to plot three graphs on one axis but why is the plot handle deleted? Even if every "hold on" in the code is active the error still exists...
Full Error: Invalid or deleted object. Error in Forum (line 214): handle_figure1_plt_legend(intCounter).Color = handle_figure1_plots(intCounter).Color;
Thank you very much,
Jonathan
%% Settings for six small diagrams
data.strSmallPlotTitles = {'1', ...
'2', ...
'3', ...
'4', ...
'5', ...
'6'};
data.strYLabel = {'Concentration [g/L]'};
% - Font style
data.strFontTitle = 'Times New Roman';
data.intFontsizeTitle = 13;
data.strFontLabel = 'Times New Roman';
data.intFontsizeLabel = 12;
data.intCultivationFinishedAfter = 24;
blnPlotSmallDiagrams = true;
intNumberOfFigure = [3 4];
%% Fake data
time = (0:0.1:data.intCultivationFinishedAfter-0.1);
% Create YOUT
dX_ae_dt = rand(length(time),1)*15;
dX_yt_dt = rand(length(time),1)*4;
dGlc_dt = rand(length(time),1)*0.5;
dO2_dt = rand(length(time),1)*10;
dCO2_dt = rand(length(time),1)*5;
dEtOH_dt = rand(length(time),1)*4;
YOUT = [dX_ae_dt ...
dX_yt_dt ...
dGlc_dt ...
dO2_dt ...
dCO2_dt ...
dEtOH_dt ...
];
%% Color matrix for the different diagrams
matColorTemplate = ['#3C8F44' % 1
'#C99D44' % 2
'#C94A2A' % 3
'#4CB1DB' % 4
'#6F878F' % 5
'#1B56D6' % 6
];
% Marker
ax6_inv_marker = '*';
ax3_inv_marker = 'd';
ax4_inv_marker = 's';
ax5_inv_marker = 'p';
ax_inv_marker = [ax6_inv_marker
ax3_inv_marker
ax4_inv_marker
ax5_inv_marker];
matMarker = ['*'
'd'
's'
'p'
'x'
'+'
];
%% Six small diagrams in one big figure
if data.blnPlotSmallDiagrams == true
matFigureExists = findobj('type','figure','name','Six individual diagrams');
% % Does a figure already exists?
% if isempty(matFigureExists) == true
% % Initialize figure handle
% h_figure1 = figure(intNumberOfFigure(2));
%
% % Half or full display size
% if data.blnPlotBigDiagram == true
% h_figure1.Position = [Pix_SS(3)/2, 50, Pix_SS(3)/2, 950]; % left bottom width high
% else
% h_figure1.Position = [1, 50, Pix_SS(3), 950]; % left bottom width high
% end
%
% % Figure settings
% h_figure1.Color = [1,1,1]; % Background color
% h_figure1.Units = 'normalized';
% h_figure1.Name = 'Six individual diagrams'; % Name of the figure
% end
% Allow multiple diagrams in one figure
tiledlayout(2,3) % Requires R2019b or later
% Initialize axes handles
h_f1_ax1 = nexttile; % Position: Row=1, Column=1
h_f1_ax2 = nexttile; % Position: Row=2, Column=1
h_f1_ax3 = nexttile; % Position: Row=1, Column=3
h_f1_ax4 = nexttile; % Position: Row=1, Column=2
h_f1_ax5 = nexttile; % Position: Row=2, Column=2
h_f1_ax6 = nexttile; % Position: Row=2, Column=3
handle_figure1_axes = [h_f1_ax1 % 1
h_f1_ax4 % 2
h_f1_ax3 % 3
h_f1_ax2 % 4
h_f1_ax5 % 5
h_f1_ax6 % 6
];
% Initialize plot handles (Visual)
h_f1_plot1 = plot(1,1);
h_f1_plot2 = plot(1,1);
h_f1_plot3 = plot(1,1);
h_f1_plot4 = plot(1,1);
h_f1_plot5 = plot(1,1);
h_f1_plot6 = plot(1,1);
handle_figure1_plots = [h_f1_plot1
h_f1_plot4
h_f1_plot3
h_f1_plot2
h_f1_plot5
h_f1_plot6
];
% Initialize plot handles (Marker)
h_f1_plt_marker1 = plot(NaN,NaN);
h_f1_plt_marker2 = plot(NaN,NaN);
h_f1_plt_marker3 = plot(NaN,NaN);
h_f1_plt_marker4 = plot(NaN,NaN);
h_f1_plt_marker5 = plot(NaN,NaN);
h_f1_plt_marker6 = plot(NaN,NaN);
handle_figure1_plt_marker = [h_f1_plt_marker1
h_f1_plt_marker2
h_f1_plt_marker3
h_f1_plt_marker4
h_f1_plt_marker5
h_f1_plt_marker6
];
% Initialize plot handles (Legend)
h_f1_plt_legend1 = plot(NaN,NaN);
h_f1_plt_legend2 = plot(NaN,NaN);
h_f1_plt_legend3 = plot(NaN,NaN);
h_f1_plt_legend4 = plot(NaN,NaN);
h_f1_plt_legend5 = plot(NaN,NaN);
h_f1_plt_legend6 = plot(NaN,NaN);
handle_figure1_plt_legend = [h_f1_plt_legend1
h_f1_plt_legend2
h_f1_plt_legend3
h_f1_plt_legend4
h_f1_plt_legend5
h_f1_plt_legend6
];
% Settings for the six small diagrams in a loop
for intCounter = 1:size(YOUT,2) % Entries of columns
hold (handle_figure1_axes(intCounter),'on');
handle_figure1_plots(intCounter) = plot(handle_figure1_axes(intCounter), ...
time, ...
YOUT(:,intCounter));
handle_figure1_axes(intCounter).XLim = [0 data.intCultivationFinishedAfter];
handle_figure1_axes(intCounter).XLabel.String = data.strXLabel{1};
handle_figure1_axes(intCounter).XLabel.FontSize = data.intFontsizeLabel;
handle_figure1_axes(intCounter).XLabel.FontName = data.strFontLabel;
handle_figure1_axes(intCounter).XTick = 0:3:data.intCultivationFinishedAfter;
handle_figure1_axes(intCounter).XGrid = 'on';
%handle_figure1_axes(intCounter).YLim = [0 YLim_calculated(intCounter)]; % Alternativ: auto
handle_figure1_axes(intCounter).YLabel.String = data.strYLabel{1};
handle_figure1_axes(intCounter).YLabel.FontSize = data.intFontsizeLabel;
handle_figure1_axes(intCounter).YLabel.FontName = data.strFontLabel;
%handle_figure1_axes(intCounter).YTick = 0:YLim_calculated(intCounter)/10:YLim_calculated(intCounter);
handle_figure1_axes(intCounter).YGrid = 'on';
handle_figure1_plots(intCounter).Color = matColorTemplate(intCounter,:);
handle_figure1_plots(intCounter).LineWidth = 1.1;
% Position handling of the XLabel and YLabel
handle_figure1_axes(intCounter).XLabel.Position(2) = handle_figure1_axes(intCounter).XLabel.Position(2) - abs(handle_figure1_axes(intCounter).XLabel.Position(2)*0.1);
handle_figure1_axes(intCounter).YLabel.Position(1) = handle_figure1_axes(intCounter).YLabel.Position(1) - abs(handle_figure1_axes(intCounter).YLabel.Position(1)*0.1);
% Titles settings
title(handle_figure1_axes(intCounter),data.strSmallPlotTitles{intCounter});
handle_figure1_axes(intCounter).Title.FontName = data.strFontTitle;
handle_figure1_axes(intCounter).Title.FontSize = data.intFontsizeTitle;
handle_figure1_axes(intCounter).Title.FontWeight = 'bold';
handle_figure1_axes(intCounter).Title.Position(2) = handle_figure1_axes(intCounter).Title.Position(2) + abs(handle_figure1_axes(intCounter).Title.Position(2)*0.01); % left bottom width high
% Plot Marker on current axis
if intCounter < 6
%hold (handle_figure1_axes(intCounter),'on')
handle_figure1_plt_marker(intCounter) = plot(handle_figure1_axes(intCounter),time(5:20:end),YOUT(5:20:end,intCounter));
handle_figure1_plt_marker(intCounter).Color = handle_figure1_plots(intCounter).Color;
handle_figure1_plt_marker(intCounter).Marker = matMarker(intCounter);
handle_figure1_plt_marker(intCounter).LineStyle = 'none';
%hold(handle_figure1_axes(intCounter),'off')
else
handle_figure1_plots(intCounter).LineStyle = '--';
%hold(handle_figure1_axes(intCounter),'off')
end
% Plot Legend on current axis
if intCounter < 6
%hold(handle_figure1_axes(intCounter),'on')
handle_figure1_plt_legend(intCounter).Color = handle_figure1_plots(intCounter).Color;
handle_figure1_plt_legend(intCounter).Marker = handle_figure1_plt_marker(intCounter).Marker;
%hold(handle_figure1_axes(intCounter),'off')
end
%hold (handle_figure1_axes(intCounter),'off')
end
end

採用された回答

Walter Roberson
Walter Roberson 2020 年 7 月 8 日
h_f1_plot1 = plot(1,1);
h_f1_plot2 = plot(1,1);
Those are on the same axes, the one you activated with
h_f1_ax6 = nexttile; % Position: Row=2, Column=3
You have no "hold on" after the first plot() call, so the second one will remove the first one.
As you are taking the time to record the axes handles, I recommend that you always use the axes handle in the plot call, and remember to hold on:
h_f1_plot1 = plot(h_f1_ax1, 1, 1);
hold(h_f1_ax1, 'on')
h_f1_plt_marker1 = plot(h_f1_ax1, NaN, NaN);
or as appropriate.
  1 件のコメント
Jonathan P
Jonathan P 2020 年 7 月 8 日
Thank you very much for the fast support! That fixed it!
Thank you for sharing your knowledge. :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by