MatLab使用“for”和“if”表示字符串

时间:2013-06-12 15:16:14

标签: string matlab text textscan

我有text.txt,其中包含单词和数字。

1。我想对包含字符串的单元格或矩阵使用“for”和“if”。

d = reshape(textread('text.txt','%s','delimiter','\t'),2,2)'
if h(1,1)=='apple'
    k=1
end

这不起作用。

2。我想做这样的事情。

for m=1:size(text)
   for n=1:size(text2)
       if text2(n,3) contains 'apple' (such as "My apple his, her")
          if last word of text2(n,3) is text(m,2)
             output(m,1)==text(m,2)
             output(m,2)==text(m,1)
          end
       end
   end
end

它是用非Matlab方式编写的,但你知道我想做什么。

如果text和text2是数字矩阵,这种类型的工作可能很容易。但它们是弦乐。

我如何执行与数字矩阵相同的操作?

1 个答案:

答案 0 :(得分:1)

我认为您正在寻找字符串操作实用程序。以下是一些:

% Case-sensitive comparison
strcmp('apple', 'apple') == true
strcmp('apple', 'orange') == false    

% Case-INsensitive comparison
strcmpi('apple', 'AppLe') == true
strcmpi('apple', 'orange') == false    

% Compare first N characters, case sensitive
strncmp('apple', 'apples are delicious', 5) == true   

% Compare first N characters, case INsensitive
strncmpi('apple', 'AppLes are delicious', 5) == true   

% find strings in other strings
strfind('I have apples and oranges', 'apple') 
ans =
    8    % match found at 8th character 

% find any kind of pattern in (a) string(s)
regexp(...) 

好吧,我甚至不会在regexp开始......只需阅读doc regexp:)