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

2024-12-21 09:12:02
推荐回答(3个)
回答1:

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

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

可以满足你的要求。

回答2:

>> 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

回答3:

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

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