【问题标题】:find index of a string in a structure field在结构字段中查找字符串的索引
【发布时间】:2020-08-27 06:16:26
【问题描述】:

我有一个包含 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】:

    差不多了。将STRUCT.LAB 包裹在一个单元格中:

    idx = find(strcmp({STRUCT.LAB},'INPUT'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-24
      • 2016-02-15
      • 1970-01-01
      • 2020-12-02
      • 2019-11-12
      • 2021-04-19
      相关资源
      最近更新 更多