结构字段中字符串的索引

问题描述 投票:0回答:1

我有一个具有12个字段的结构STRUCT(1x70结构)。第一字段LAB在每个单元格中包含字符串(例如'ab1','fj3','INPUT','OUTPUT'等)。我需要找到字符串'INPUT'的索引。

STRUCT = struct('LAB',{'ab1', 'fj3', 'INPUT', 'OUTPUT'},'fieldname2',{10,32,53,14})

我已经尝试使用以下代码行,但是每行都不起作用。

idx = strfind(STRUCT.LAB, 'INPUT'); %Error using strfind Unrecognized parameter name 'INPUT'.

idx = ([STRUCT.LAB]=='INPUT') %Matrix dimensions must agree.

idx = find(strcmp([STRUCT.LAB], 'INPUT')) %ans: [] rather than 3.
string matlab indexing structure
1个回答
2
投票

几乎在那里。将STRUCT.LAB包裹在一个单元格中:

idx = find(strcmp({STRUCT.LAB},'INPUT'))
© www.soinside.com 2019 - 2024. All rights reserved.