MATLAB如何半段一个字符数组中含有特定字符?

比如我有一个字符数组str,里面有很多字符,我想判断这个数组里是否含有JPG这个字符串,JPG是连在一起的,如果有,则返回1,没有就返回0。请问MATLAB有现成的命令么?如果自己写,应该怎么写?谢谢大家。

在matlab中有查找字符串的命令,但是不是你所要求的返回1或0;
你可以使用如下的命令:

~isempty(strfind(str,'JPG'))

可以满足你的要求。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-04-09
>> help strfind
STRFIND Find one string within another.
K = STRFIND(TEXT,PATTERN) returns the starting indices of any
occurrences of the string PATTERN in the string TEXT.

STRFIND will always return [] if PATTERN is longer than TEXT.

Examples
s = 'How much wood would a woodchuck chuck?';
strfind(s,'a') returns 21
strfind('a',s) returns []
strfind(s,'wood') returns [10 23]
strfind(s,'Wood') returns []
strfind(s,' ') returns [4 9 14 20 22 32]

See also strcmp, strncmp, regexp.

Overloaded methods:
cell/strfind

Reference page in Help browser
doc strfind

如果是针对文件名,这个或许也有帮助

>> help fileparts
FILEPARTS Filename parts.
[PATHSTR,NAME,EXT] = FILEPARTS(FILE) returns the path,
filename and extension for the specified file.
FILEPARTS is platform dependent.

You can reconstruct the file from the parts using
fullfile(pathstr,[name ext versn])

The fourth output, VERSN, of FILEPARTS will be removed in a future
release.

See also fullfile, pathsep, filesep.

Reference page in Help browser
doc fileparts
第2个回答  推荐于2016-10-03

    使用strfind函数。
    S = 'Find the starting indices of the pattern string';
    strfind(S, 'in')
    ans =
         2    15    19    45

    使用regexp函数。
    str = 'bat cat can car COAT court cut ct CAT-scan';
    regexp(str, 'c[aeiou]+t')
    ans =
         5    28