Subindex error when using with graphics

1 回表示 (過去 30 日間)
Ziv Kassner
Ziv Kassner 2018 年 1 月 10 日
コメント済み: Ziv Kassner 2018 年 1 月 10 日
When I run this:
ind = findobj(dig_data.objects.left_wing.points);
non_zero_handles = dig_data.objects.left_wing.points(ind);
I get an error: Function 'subsindex' is not defined for values of class 'matlab.graphics.chart.primitive.Line'.
Where 'dig_data' is a struct and 'points' is a line, and I run the code with matlab 2017 and the code itself was written for matlab older than 2014.
Thank you, Ziv

採用された回答

Walter Roberson
Walter Roberson 2018 年 1 月 10 日
That code was always incorrect.
Starting in R2014b, findobj() returns handle objects rather than graphic handles that are represented by doubles that were used before. Your code attempts to use those handle objects as an index into an array, and the error message is telling you that those kinds of objects cannot be used as indices.
When graphics objects were represented by doubles, then only figure handles could be integers, with line objects always having a non-zero fraction. Therefore the doubles that represented line objects were never valid indices in those earlier releases.
The code would have made sense before if it had been
ind = find(dig_data.objects.left_wing.points);
non_zero_handles = dig_data.objects.left_wing.points(ind);
in which case it would have been suitable for the case where some items in the points array were stored as 0 as placeholders representing non-existent lines and the code wanted to extract just the used items.
Assuming that someone might have assigned directly to indices in the points vector, leaving 0's for the unused slots in earlier MATLAB and leaving placeholder objects in current releases, then the current code would be
ind = isvalid(dig_data.objects.left_wing.points);
non_zero_handles = dig_data.objects.left_wing.points(ind);
and both versions would work (under those assumptions) with
ind = ishandle(dig_data.objects.left_wing.points);
non_zero_handles = dig_data.objects.left_wing.points(ind);
if I recall correctly.
Using ishandle() will not work correctly for the case where the points vector contains some objects that were initialized but the objects were deleted.
  1 件のコメント
Ziv Kassner
Ziv Kassner 2018 年 1 月 10 日
isvalid worked well!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by