Problem while Saving/Copying graphic handles to an array
1 回表示 (過去 30 日間)
古いコメントを表示
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 件)
Walter Roberson
2021 年 11 月 11 日
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
cellfun(@isvalid, h_circles)
arrayfun(@isvalid, handles.axes1.Children)
arrayfun(@isvalid, handles.axes2.Children)
% 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 件のコメント
Walter Roberson
2021 年 11 月 12 日
I do not see a hold call there? As I emphasized earlier, the hold statement is the key.
参考
カテゴリ
Help Center および File Exchange で Computer Vision with Simulink についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!