MATLAB Answers

Why is my code showing an error? Why is the line deleted?

9 views (last 30 days)
JP on 23 Nov 2019
Commented: Jan on 23 Nov 2019
This is my code to fill out boxplots and to visualize the mean value as a cross 'x'.
h = findobj(gca,'Tag','Box');
for p=1:length(h)
eval(['meanLocal = mean_',stain,'_',orien,'_',magn,'_',comp,'(p,1)'])
After a few iterations I get the following error:
Error using
Invalid or deleted object.
Error in boxplotting2 (line 77)
I can't seem to figure out why h(p) is a handle to a deleted line. There is nothing in my code that could delete the content.
It would be greatly appreciated if someone could help me with this issue.

Answers (1)

Jan on 23 Nov 2019
The shown code does not reproduce the problem. We cannot guess reliably, why one of the obejcts in h is deleted. But a weak guess:
plot is a hight level command, which clears the current axes object before inserting a new element - except if you have called hold('on') before or set the 'NextPlot' property to 'add'.
The eval command is a really bad design. See TUTORIAL: how and why to avoid EVAL
Jan on 23 Nov 2019
@Image Analyst: With fprintf the value of meanLocal is not adjusted. Unfortunately the eval() is really needed, because the OP has stored the variables with encodeing the parameters in the name of the variable:
meanLocal = mean_STAIN_ORIEN_MAGN_COMP(p,1)
% where the uppercase parts are variable

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!