필터 지우기
필터 지우기

Filtering array of custom objects

조회 수: 6 (최근 30일)
Timo Kuchheuser
Timo Kuchheuser 2018년 11월 25일
편집: Sebastian Tilders 2022년 7월 23일
Hey!
I was wondering if it is possible to implement the filtering of arrays as describes here: Find Array Elements That Meet a Condition for an array holding objects of a custom class.
I have a class with the following property
properties (Access = public)
stages (1,:) EnvelopeStage
end
An EnvelopeStage object has the following properties
properties (GetAccess = public, SetAccess = public)
name string
duration double {mustBeFinite, mustBeNonnegative, mustBeLessThanOrEqual(duration,1)} = 0
target_level double {mustBeFinite, mustBeNonnegative, mustBeLessThanOrEqual(target_level,1)} = 0
end
What would be the smartes way (besides iterating over the array) to filter by name?
Is there a way to implement or use a function that yields the elemtens with code like this:
stages(stages.name == 'foo');
Thank you very much
Tim

채택된 답변

Honglei Chen
Honglei Chen 2018년 11월 26일
You can probably try
idx = arrayfun(@(x)strcmp(x.name,'foo'),stages);
stages(idx);
HTH
  댓글 수: 2
Timo Kuchheuser
Timo Kuchheuser 2018년 11월 26일
That worked like a charm!
Thank you very much!
Sebastian Tilders
Sebastian Tilders 2022년 7월 23일
편집: Sebastian Tilders 2022년 7월 23일
This does not work, as it does not return indexes. It return strcmp results. stages(idx == 1) works for me.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matched Filter and Ambiguity Function에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by