Get index of object with a given property in an array

8 ビュー (過去 30 日間)
Nicolas Bourbaki
Nicolas Bourbaki 2020 年 3 月 18 日
コメント済み: Nicolas Bourbaki 2020 年 3 月 19 日
Is it possible to remove an entry from a vector of arrays based on a certain property?
How would I get the index of a certain person in the n the phone book example on https://de.mathworks.com/help/matlab/matlab_oop/finding-objects-having-specific-settings.html?
classdef PhoneBook < dynamicprops
properties
Name
Address
Number
end
methods
function obj = PhoneBook(n,a,p)
obj.Name = n;
obj.Address = a;
obj.Number = p;
end
end
end
We add some entries:
PB(1) = PhoneBook('Nancy Vidal','123 Washington Street','5081234567');
PB(2) = PhoneBook('Nancy Vidal','123 Main Street','5081234568');
PB(3) = PhoneBook('Nancy Wong','123 South Street','5081234569');
I would now like to remove an entry based on the value of a property:
NW = findobj(PB,'Name','Nancy Wong'); % To remove an entry, I would like to get the index and not the entry itself

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 3 月 18 日
For the class you defined, you can use something like this
PB(1) = PhoneBook('Nancy Vidal','123 Washington Street','5081234567');
PB(2) = PhoneBook('Nancy Vidal','123 Main Street','5081234568');
PB(3) = PhoneBook('Nancy Wong','123 South Street','5081234569');
NW = findobj(PB,'Name','Nancy Wong');
PB(PB == NW) = [];
  1 件のコメント
Nicolas Bourbaki
Nicolas Bourbaki 2020 年 3 月 19 日
This was exactly what I was looking for.
Thanks a lot!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeClass Introspection and Metadata についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by