(Matlab)如何检查单元格数组是否包含字符串 [英] (Matlab) How to check if cell array contains string

查看:856
本文介绍了(Matlab)如何检查单元格数组是否包含字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Excel电子表格中获取数据,并且仅从与字符串匹配的单元格中获取信息.例如.如果单元格A10中包含加拿大"一词,则应返回该单元格.

I am trying to grab data from an excel spread sheet and grab only the information from cells that match a string. Eg. if cell A10 contains the word 'Canada' it should return that cell.

我尝试使用strcmp( https://www.mathworks.com /help/matlab/ref/strcmp.html )来检查参数1中的字符串是否包含在包含许多字符串的单元格数组中,第二个参数是

I have tried using strcmp(https://www.mathworks.com/help/matlab/ref/strcmp.html) to check if string in argument 1 is contained in a cell array containing many strings, the second argument

[num,txt,raw] = xlsread('\\Client\C$\Users\Fish\Desktop\dataset\dataset.csv');

mytable = cell(raw); 


for i = 1:54841 
    array_index = i;
    string_index = mytable(i,2);
    string_eastern = {'Canada', 'Ontario'};

    if strcmp(string_index,string_eastern);
       fprintf('%d\n',array_index)
    end
end

在上面的示例中,如果我的string_eastern仅包含一个元素,例如"Canada",它将返回"Canada"的每个实例的索引值.如果我添加更多元素,我希望它将为string_index与string_eastern中包含的字符串匹配的每个实例返回索引值.但是,如果添加更多元素,我将一无所获.

In the above example if my string_eastern only contains one element, say 'Canada', it will return the index value of every instance of 'Canada'. If I add more elements I expect it would return index values for every instance where string_index would match with a string contained in string_eastern. However I get no results at all if I add more elements.

我几乎想检查一下我的string_index是否为string_eastern,如果值匹配,那么我希望它返回该单元格值.当string_eastern仅是1个元素但不能使用超过1个元素时,此方法有效.

Pretty much I wanted to check my string_index agaisnt string_eastern, if the values match then I want it to return that cell value. This works when string_eastern is only 1 element but does not work with more than 1.

推荐答案

要访问单元格内容,请使用大括号{}.因此,如果我想访问单元格的第一个元素,我会这样说:

To access cell contents, use the curly brackets {}. So if I wanted to access the first element of my cell, I would say this:

string = cell{1};

进一步了解有关单元格的MATLAB文档,以了解更多信息并回答您的其他任何问题.

Read more on the MATLAB Documentation about cells to learn more and to answer any of your further questions.

这篇关于(Matlab)如何检查单元格数组是否包含字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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