Problem while Saving/Copying graphic handles to an array

1 view (last 30 days)
I am trying to save n number of graphic handles to a (cell)array in a loop. (so that I can change each item visibility later using array index)
The problem I face:
Every time the loop iterate and add a new element to the array, element in the previous index is becoming invalid. (eg: when h_circles(2) is added, h_circles(1) handle values are lost)
Finally When I try to access or modify any handle in h_circles , "invalid or deleted handle" error is occurring.
IS cell array the correct method to save graphic handles for later use?? If not how to store handles for later use?
Here is the sample code snippet:
% Matlab2019
global h_circles; % global because I want to use the handles in other (callback)functions
n = 5; % number of concentric-circles
x = 0;
y = 0;
r = 10;
h_circles = cell(1, n); % making it cell array
for i = 1 : n
h_circles(1, i) = {drawcircle(x, y, r)};
r = r + 10;
end
% any circle drawing algo
function h = drawcircle(x,y,r)
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
h = plot(xunit, yunit);
hold off
  1 Comment
Vyshakh Pv
Vyshakh Pv on 11 Nov 2021
Edited: Vyshakh Pv on 11 Nov 2021
% I tried makeing h_circles gobjects
h_circles = gobjects(1,n);
%and
h_circles(i) = {copyobj(drawcircle(x, y, r), handles.axes1)};
Not working...

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 11 Nov 2021
Your code works for me.
The important point for this purpose is one that you already took care of: you need to have hold turned on in the axes being drawn into with plot() commands. You already do that, so it works out. If you did not have the hold in your drawcircles function, you would have the kinds of problems you describe.
fig = figure();
handles.axes1 = axes(fig);
handles.axes2 = axes(fig); %this is the one that will be drawn in
% Matlab2019
global h_circles; % global because I want to use the handles in other (callback)functions
n = 5; % number of concentric-circles
x = 0;
y = 0;
r = 10;
h_circles = cell(1, n); % making it cell array
for i = 1 : n
h_circles(1, i) = {copyobj(drawcircle(x, y, r), handles.axes1)}
r = r + 10;
end
h_circles = 1×5 cell array
{1×1 Line} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
h_circles = 1×5 cell array
{1×1 Line} {1×1 Line} {0×0 double} {0×0 double} {0×0 double}
h_circles = 1×5 cell array
{1×1 Line} {1×1 Line} {1×1 Line} {0×0 double} {0×0 double}
h_circles = 1×5 cell array
{1×1 Line} {1×1 Line} {1×1 Line} {1×1 Line} {0×0 double}
h_circles = 1×5 cell array
{1×1 Line} {1×1 Line} {1×1 Line} {1×1 Line} {1×1 Line}
cellfun(@isvalid, h_circles)
ans = 1×5 logical array
1 1 1 1 1
arrayfun(@isvalid, handles.axes1.Children)
ans = 5×1 logical array
1 1 1 1 1
arrayfun(@isvalid, handles.axes2.Children)
ans = 5×1 logical array
1 1 1 1 1
% any circle drawing algo
function h = drawcircle(x,y,r)
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
h = plot(xunit, yunit);
hold off
end
  2 Comments
Vyshakh Pv
Vyshakh Pv on 12 Nov 2021
Hai Walter Roberson thanks for the reply...
I know it’s weird, the above sample code works for me too. But when I implement the same in my project, it does not.
I have shared a video link to see it yourself. Also my project code portion.
Video Link:
HandleArray_ScreenRecord In this video you can see the elements in h_airTarg_path, h_airTarg_label, h_circles getting deleted when new elements are added. ( https://youtu.be/DvwTt7RokRc )
Project code snippet:
Walter Roberson
Walter Roberson on 12 Nov 2021
I do not see a hold call there? As I emphasized earlier, the hold statement is the key.

Sign in to comment.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!