Indexing into a structure to retain/remove elements based on a string

I have a variable containing a string for one eye (i.e. selectedEye = LEFT) and a struct called "x" with 2 fields:
x =
eye: {1x1938 cell}
timestamp: [1x1938 double]
Each cell in the field "eye" contains the string "LEFT" or "RIGHT". What code would I use to retain cells and corresponding timestamps for just the left eye (i.e. deleting eye/timestamp entries for one eye)?
I tried the following to try to obtain a logical:
x.eye = x.eye(regexp(cellstr(selectedEye),x.eye))
...but received an error ("Function 'subsindex' is not defined for values of class 'cell'.")

 採用された回答

Bruno Luong
Bruno Luong 2018 年 9 月 18 日

0 投票

retain = strcmpi(X.eye,'LEFT')); % selectedEye is 'LEFT'
X.eye = X.eye(retain); % somewhat useless assignment
X.timestamp= X.timestamp(retain);

4 件のコメント

Al_G
Al_G 2018 年 9 月 18 日
Thanks! I thought I might be overcomplicating it, but couldn't figure out how.
Could you clarify your comment about the useless assignment? I'm not sure I understand it.
Bruno Luong
Bruno Luong 2018 年 9 月 18 日
It hurts my eye to see 1000 cell string with 'LEFT' inside all of them.
James Tursa
James Tursa 2018 年 9 月 18 日
I agree with Bruno. Since you already know exactly what is in X.eye by the way you constructed it, do you really even need it at all?
Al_G
Al_G 2018 年 9 月 19 日
Very good point, it's gratuitous. I'll remove it completely. Thanks!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSearch Path についてさらに検索

タグ

質問済み:

2018 年 9 月 18 日

コメント済み:

2018 年 9 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by