MATLAB使用包含IF语句中的函数

问题描述 投票:0回答:2

我有一个单元格数组,在MATLAB中有几个月的字符串。我使用contains()函数来确定'Ju'是否在字符串中,以及if语句,如果该语句为true,则删除'Ju。'但是我收到了一个错误。错误是:未定义的函数或变量'a'。

如果a ==包含(月份,模式),则无标题(第4行)出错

pattern = 'Ju';
months = {'June', 'July', 'August', 'September'};

if a == contains(months, pattern)
    a = regexprep(months, 'Ju', '')
end
matlab if-statement condition
2个回答
1
投票

你需要在测试之前设置一个:

>> a = contains(months, pattern)

a =

  1×4 logical array

  1   1   0   0

2
投票

对于您的示例,您甚至不需要if语句。 regexprep可以为您处理:

>> pattern = 'Ju';
>> months = {'June', 'July', 'August', 'September'};
>> a = regexprep(months, pattern, '')

a =

  1×4 cell array

    'ne'    'ly'    'August'    'September'
© www.soinside.com 2019 - 2024. All rights reserved.