endsWith
R2026bDetermine if strings or category names end with pattern
Description
Examples
Create a string array that contains file names. Determine which file names end with the .gz extension.
str = ["abstract.docx","data.tar.gz","mycode.m"; ... "data-analysis.ppt","results.ptx","temp-archive.gz"]
str = 2×3 string
"abstract.docx" "data.tar.gz" "mycode.m"
"data-analysis.ppt" "results.ptx" "temp-archive.gz"
Return a logical array where the position of each element equal to 1 corresponds to the position of a string in str that ends with .gz.
pat = ".gz";
TF = endsWith(str,pat)TF = 2×3 logical array
0 1 0
0 0 1
Display the file names that end with .gz. Index back into str using TF.
str(TF)
ans = 2×1 string
"data.tar.gz"
"temp-archive.gz"
Create a string array of file and folder names, where some names have extensions.
str = ["abstract.docx","data.tar.gz","REPORTS"; ... "data-analysis.ppt","results.ptx","ARCHIVE"]
str = 2×3 string
"abstract.docx" "data.tar.gz" "REPORTS"
"data-analysis.ppt" "results.ptx" "ARCHIVE"
To find names that end with extensions, create a pattern that matches a period followed by letters by using the lettersPattern function. (You can build up a complex pattern by combining simple patterns in expressions. Such expressions can also include literal text, like "." in this example.)
pat = "." + lettersPatternpat = pattern
Matching:
"." + lettersPattern
Return a logical array indicating which names end with extensions.
TF = endsWith(str,pat)
TF = 2×3 logical array
1 1 0
1 1 0
Display the matching names.
str(TF)
ans = 4×1 string
"abstract.docx"
"data-analysis.ppt"
"data.tar.gz"
"results.ptx"
Find the names with extensions that are exactly three letters long.
pat = "." + lettersPattern(3);
TF = endsWith(str,pat);
str(TF)ans = 2×1 string
"data-analysis.ppt"
"results.ptx"
For a list of functions that create pattern objects, see pattern.
Create a string array that contains file names. Determine which file names end with the .docx, .xlsx, or .gz extensions.
str = ["data.tar.gz","mycode.m","outputs.xlsx","results.pptx"]
str = 1×4 string
"data.tar.gz" "mycode.m" "outputs.xlsx" "results.pptx"
pat = [".docx",".xlsx",".gz"]; TF = endsWith(str,pat)
TF = 1×4 logical array
1 0 1 0
Display the file names that end with .docx, .xlsx, or .gz. Index back into str using TF.
str(TF)
ans = 1×2 string
"data.tar.gz" "outputs.xlsx"
Create a string array that contains file names. Determine which file names end with the .gz extension, ignoring case.
str = ["DATA.TAR.GZ","mycode.m","SUMMARY.PPT","tmp.gz"]
str = 1×4 string
"DATA.TAR.GZ" "mycode.m" "SUMMARY.PPT" "tmp.gz"
pattern = ".gz";
TF = endsWith(str,pattern,IgnoreCase=true)TF = 1×4 logical array
1 0 0 1
Display the file names that end with .gz. Index back into str using TF.
str(TF)
ans = 1×2 string
"DATA.TAR.GZ" "tmp.gz"
Create a character vector that contains the name of a file. Determine whether the name ends with specified extensions.
chr = 'MyLatestPaper.docx'chr = 'MyLatestPaper.docx'
TF = endsWith(chr,'docx')TF = logical
1
TF = endsWith(chr,'gz')TF = logical
0
Since R2026b
Load data on electric utility outages in the United States as a table. Convert the Region variable to a categorical array.
T = readtable("outages.csv");
T.Region = categorical(T.Region);Determine if the category names of Region end with "West". The endsWith function returns a logical array indicating which elements in the Region categorical array have category names that end with "West".
TF = endsWith(T.Region,"West",IgnoreCase=true)TF = 1468×1 logical array
1
0
0
1
1
1
1
1
0
1
0
1
0
0
1
⋮
Display only the rows of the table where the region ends with "West".
T(TF,:)
ans = 522×6 table
Region OutageTime Loss Customers RestorationTime Cause
_________ ________________ ______ __________ ________________ ___________________
SouthWest 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 {'winter storm' }
West 2004-04-06 05:44 434.81 3.4037e+05 2004-04-06 06:10 {'equipment fault'}
MidWest 2002-03-16 06:18 186.44 2.1275e+05 2002-03-18 23:23 {'severe storm' }
West 2003-06-18 02:49 0 0 2003-06-18 10:54 {'attack' }
West 2004-06-20 14:39 231.29 NaN 2004-06-20 19:16 {'equipment fault'}
West 2002-06-06 19:28 311.86 NaN 2002-06-07 00:51 {'equipment fault'}
MidWest 2004-09-27 11:09 286.72 66104 2004-09-27 16:37 {'equipment fault'}
West 2004-05-21 21:45 159.99 NaN 2004-05-22 04:23 {'equipment fault'}
West 2003-11-12 06:12 254.09 9.2429e+05 2003-11-17 02:04 {'winter storm' }
West 2004-12-21 18:50 112.05 7.985e+05 2004-12-29 03:46 {'winter storm' }
West 2002-12-16 13:43 70.752 4.8193e+05 2002-12-19 09:38 {'winter storm' }
MidWest 2005-02-04 08:18 NaN NaN 2005-02-04 19:51 {'attack' }
MidWest 2002-03-26 01:59 388.04 5.6422e+05 2002-03-28 19:55 {'winter storm' }
SouthWest 2004-06-06 05:27 559.41 2.19e+05 2004-06-06 05:55 {'equipment fault'}
West 2005-06-29 08:37 601.13 32005 2005-06-29 08:57 {'equipment fault'}
West 2003-04-14 07:11 276.41 1.5647 2003-04-14 08:52 {'equipment fault'}
⋮
Input Arguments
Input array, specified as a string array, character vector, cell array of character vectors, or categorical array (since R2026b).
Search pattern, specified as one of the following:
String array
Character vector
Cell array of character vectors
patternarray
Extended Capabilities
The
endsWith function fully supports tall arrays. For more
information, see Tall Arrays.
Usage notes and limitations:
strandpatmust be a string scalar, a character vector, or a cell array containing not more than one character vector.
Refer to the usage notes and limitations in the C/C++ Code Generation section. The same usage notes and limitations apply to GPU code generation.
The endsWith
function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
The
endsWith function fully supports distributed arrays. For more
information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2016bThe endsWith function now supports categorical arrays as the
input array. You can use this function to determine whether elements in a
categorical array have category names that end with a specified pattern.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)