ismember input - strings vs numbers
显示 更早的评论
Hello,
I am using "ismember" to pullout data structured as a dataset.
- I am able to do both of these commands:
ismember(data.dataTag,0:24);
ismember(data.dataTag,{'A' 'B'});
- I would like to automate this process by feeding "ismember" a blanket series of criterion and run through all my "dataTag" of my data. I however run into trouble when I do this:
ismember(data.(dataTag{n}),{{'A' 'B'} 0:24});
ismember(data.(dataTag{n}),[{'A' 'B'} 0:24]);
The error I get is "Input must be cell arrays of strings."
Can this be done? Thank you for any help.
Matthieu
回答(3 个)
Honglei Chen
2011-9-6
Hi Matthieu,
Are the criterion pre-defined? If so, how about concatenating them with logical operator, e.g.,
ismember((data.(dataTag{n}),{'A' 'B'}) || ismember((data.datqaTag{n}),0:24)
Honglei Chen
2011-9-6
0 个投票
Hi Matthieu,
I think ismember(A,B) requires A and B to be the same data type. One possible solution is to use num2str to build criterion so that criterion are all strings. Then in your function, you can use isnumeric or ischar to test the data. If the data is a number, you can do num2str again before you call ismember. However, you'll have to convert it back to number for your computation. I'm not sure if it's worthy though.
BTW, I'm also confused by you code snippet. If you need to do something like sum(c), then can c be strings? I know sum will work on strings but is that what you want?
HTH
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!