startsWith
R2026bDetermine if strings or category names start with pattern
Description
Examples
Create a string array that contains file names. Determine which file names start with the word data.
str = ["abstract.docx","data.tar","code.m"; ... "data-analysis.ppt","results.ptx","summary.ppt"]
str = 2×3 string
"abstract.docx" "data.tar" "code.m"
"data-analysis.ppt" "results.ptx" "summary.ppt"
Return a logical array where the position of each element equal to 1 corresponds to the position of a string in str that starts with data.
pat = "data";
TF = startsWith(str,pat)TF = 2×3 logical array
0 1 0
1 0 0
Display the file names that start with data. Index back into str using TF.
str(TF)
ans = 2×1 string
"data-analysis.ppt"
"data.tar"
Create a string array that has references to files, including full paths for local files and URLs for remote ones.
str = ["C:\Temp\MyReport.docx"; "D:\Data\Experiment1\Trial1\Sample1.csv"; "https://example.com/Slides.pptx"]
str = 3×1 string
"C:\Temp\MyReport.docx"
"D:\Data\Experiment1\Trial1\Sample1.csv"
"https://example.com/Slides.pptx"
To find the paths that start with a drive letter, create a pattern that matches one letter followed by a colon.
pat = lettersPattern(1) + ":"pat = pattern
Matching:
lettersPattern(1) + ":"
Determine which elements of str start with that pattern. The pattern pat does not match "https:" because lettersPattern(1) matches only one letter.
TF = startsWith(str,pat)
TF = 3×1 logical array
1
1
0
Display the matching file names.
str(TF)
ans = 2×1 string
"C:\Temp\MyReport.docx"
"D:\Data\Experiment1\Trial1\Sample1.csv"
For a list of functions that create pattern objects, see pattern.
Create a string array that contains file names. Determine which file names start with either abstract or data.
str = ["abstract.docx","data.tar.gz","mycode.m","results.ptx"]
str = 1×4 string
"abstract.docx" "data.tar.gz" "mycode.m" "results.ptx"
pat = ["abstract","data"]; TF = startsWith(str,pat)
TF = 1×4 logical array
1 1 0 0
Display the strings that start with either abstract or data. Index back into str using TF.
str(TF)
ans = 1×2 string
"abstract.docx" "data.tar.gz"
Create a string array that contains file names. Determine which file names start with data, ignoring case.
str = ["DATA.TAR.GZ","data.xlsx","SUMMARY.PPT","tmp.gz"]
str = 1×4 string
"DATA.TAR.GZ" "data.xlsx" "SUMMARY.PPT" "tmp.gz"
pat = "data";
TF = startsWith(str,pat,IgnoreCase=true)TF = 1×4 logical array
1 1 0 0
Display the strings that start with data. Index back into str using TF.
str(TF)
ans = 1×2 string
"DATA.TAR.GZ" "data.xlsx"
Create a character vector that contains the name of a file. Determine if the name starts with different substrings.
chr = 'data-analysis.ppt'chr = 'data-analysis.ppt'
TF = startsWith(chr,'data')TF = logical
1
TF = startsWith(chr,'test')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 start with "South". The startsWith function returns a logical array indicating which elements in the Region categorical array have category names that start with "South".
TF = startsWith(T.Region,"South",IgnoreCase=true)TF = 1468×1 logical array
1
1
1
0
0
0
0
0
0
0
1
0
1
1
0
⋮
Display only the rows of the table where the region starts with "South".
T(TF,:)
ans = 415×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' }
SouthEast 2003-01-23 00:49 530.14 2.1204e+05 NaT {'winter storm' }
SouthEast 2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 {'winter storm' }
SouthEast 2004-09-05 17:48 73.387 36073 2004-09-05 20:46 {'equipment fault'}
SouthEast 2002-09-01 18:22 95.917 36759 2002-09-01 19:12 {'severe storm' }
SouthEast 2003-09-27 07:32 NaN 3.5517e+05 2003-10-04 07:02 {'severe storm' }
SouthEast 2004-12-06 23:18 NaN 37136 2004-12-14 03:21 {'winter storm' }
SouthEast 2002-12-12 18:08 46.918 1.0698e+05 2002-12-14 18:43 {'winter storm' }
SouthEast 2005-03-08 16:37 1339.2 4.3003e+05 2005-03-10 20:42 {'winter storm' }
SouthEast 2003-02-24 06:13 0 0 2003-02-24 21:18 {'attack' }
SouthEast 2005-05-07 00:07 65.95 47140 2005-05-07 09:26 {'thunder storm' }
SouthWest 2004-06-06 05:27 559.41 2.19e+05 2004-06-06 05:55 {'equipment fault'}
SouthEast 2003-04-13 01:58 124.76 1.0142 2003-05-20 11:09 {'unknown' }
SouthEast 2002-07-14 21:32 90.83 60133 2002-07-14 23:53 {'thunder storm' }
SouthWest 2004-07-18 14:40 340.35 1.4963e+05 2004-07-26 23:34 {'severe storm' }
SouthEast 2003-09-05 20:15 1700.1 1.6393e+05 2003-09-10 19:59 {'thunder storm' }
⋮
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
startsWith function fully supports tall arrays. For more
information, see Tall Arrays.
Usage notes and limitations:
strandpatternmust 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 startsWith
function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
The
startsWith function fully supports distributed arrays. For more
information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2016bThe startsWith 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 start 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)