From e4a80034a1ebf606711058151021a5123e3d60de Mon Sep 17 00:00:00 2001 From: chunyu Date: Sun, 23 Jun 2024 17:18:35 +0800 Subject: [PATCH] update legend callback function --- content/posts/matlab/legend-callback/index.md | 11 +++++++++++ content/posts/matlab/legend-callback/test_demo.m | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/content/posts/matlab/legend-callback/index.md b/content/posts/matlab/legend-callback/index.md index 39ab980f..97c0b1fd 100644 --- a/content/posts/matlab/legend-callback/index.md +++ b/content/posts/matlab/legend-callback/index.md @@ -153,7 +153,18 @@ function my_legend_callback(hSrc,eventData) % mid click to show only the selected object or show all if strcmp(eventData.SelectionType, 'extend') && strcmp(eventData.Region, 'icon') % get all graphic objects displayed in legend + % results may contain 'group' object create by 'bode', ignore their children go = findobj(eventData.Peer.Parent, '-property', 'DisplayName'); + K = length(go); + idx = true(K,1); + for k = 1:K + if isprop(go(k), 'Children') + if isprop(go(k).Children, 'Visible') + idx(go == go(k).Children) = false; + end + end + end + go = go(idx); go_visible = [go.Visible]; if sum(go_visible) == 1 && isequal(go(go_visible), eventData.Peer) for k = 1:length(go) diff --git a/content/posts/matlab/legend-callback/test_demo.m b/content/posts/matlab/legend-callback/test_demo.m index d6541ea4..240336f4 100644 --- a/content/posts/matlab/legend-callback/test_demo.m +++ b/content/posts/matlab/legend-callback/test_demo.m @@ -37,7 +37,18 @@ function my_legend_callback(hSrc,eventData) % mid click to show only the selected object or show all if strcmp(eventData.SelectionType, 'extend') && strcmp(eventData.Region, 'icon') % get all graphic objects displayed in legend + % results may contain 'group' object create by 'bode', ignore their children go = findobj(eventData.Peer.Parent, '-property', 'DisplayName'); + K = length(go); + idx = true(K,1); + for k = 1:K + if isprop(go(k), 'Children') + if isprop(go(k).Children, 'Visible') + idx(go == go(k).Children) = false; + end + end + end + go = go(idx); go_visible = [go.Visible]; if sum(go_visible) == 1 && isequal(go(go_visible), eventData.Peer) for k = 1:length(go)