Struct中的字符串比较(Matlab) [英] String compare in Struct (Matlab)

查看:188
本文介绍了Struct中的字符串比较(Matlab)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要搜索一个大的结构,并找到所有具有相同名称的组件的索引.

I need search a large struct and find the index of all the components with the same name.

例如:如果名称为13hy,则需要返回数组[1,5]

For example: If the name is 13hy I need an array [1,5] returned

 structure(1,1).name = '13hy'
 structure(2,1).name = '64hy'
 structure(3,1).name = '37hy'
 structure(4,1).name = '07hy'
 structure(5,1).name = '13hy'

我尝试过:

 strcmp(structure.name,'13hy')
 ismember(structure.name,'13hy')
 strfind(structure.name,'13hy')

,我不断收到错误消息输入参数过多". 请帮助

and I keep getting the error 'Too many input arguments.' Please help

推荐答案

使用 arrayfun 使用 匿名函数进行测试所需的名称:

Use arrayfun to traverse the structure, using a strcmp-based anonymous function to test for the desired name:

find(arrayfun(@(n) strcmp(structure(n).name, '13hy'), 1:numel(structure)))

这篇关于Struct中的字符串比较(Matlab)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆