How to find an index of an object from object array?

2 ビュー (過去 30 日間)
Nirmal Hirani
Nirmal Hirani 2022 年 10 月 13 日
コメント済み: Nirmal Hirani 2022 年 10 月 15 日
classdef data
properties (SetAccess = 'public' ,GetAccess = 'public')
Name
end
end
d1 = data();
d2 = data();
d3 = data();
d1.Name = 'data1';
d2.Name = 'data2';
d2.Name = 'data3';
% object array
mylist = [d1 d2 d3]
% asking user to input a name
userInput = 'data2'
% return a index number from 'mylist' based on the name of "userInput"
% ??
I need to check "mylist" and go through objects in that list and check which object has the name == userInput and return the index of that object. In this case return 2

採用された回答

Matt J
Matt J 2022 年 10 月 13 日
編集済み: Matt J 2022 年 10 月 13 日
One way:
d1.Name = 'data1';
d2.Name = 'data2';
d3.Name = 'data3';
d4.Name = 'data4';
d5.Name = 'data5';
d6.Name = 'data6';
mylist = [d1 d2 d3 d4 d5 d6];
userInput = 'data2';
strcmp( {mylist.Name}, userInput)
ans = 1×6 logical array
0 1 0 0 0 0
  1 件のコメント
Nirmal Hirani
Nirmal Hirani 2022 年 10 月 15 日
perfect! thank you! :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Analytics Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by