genvarname — Construct valid, unique MATLAB variable names from text.
genvarname(str) converts text into valid MATLAB variable names and makes names unique within the returned set. genvarname(str, exclusions) also avoids names listed in exclusions.
Syntax
varname = genvarname(str)
varname = genvarname(str, exclusions)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
str | Any | Yes | — | Input text, string array, character array, or cell array of text scalars. |
exclusions | Any | No | — | Names that generated output must not match. |
Returns
| Name | Type | Description |
|---|---|---|
varname | Any | Valid variable name text with the same container form as the input. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:genvarname:InvalidInput | Input is not text-compatible or cell elements are not text scalars. | genvarname: input must be text or a cell array of text |
RunMat:genvarname:InvalidOption | More than one exclusions argument is supplied. | genvarname: expected str or str,exclusions |
RunMat:genvarname:Internal | Internal string, cell, or character array construction failed. | genvarname: internal error |
How genvarname works
- Character row-vector input returns a character row vector.
- String scalar input returns a string scalar.
- String arrays preserve their shape and return string arrays.
- Cell arrays of character vectors or string scalars preserve their shape and return cell arrays of character row vectors.
- Character array input must be a character vector; use a string array or cell array to generate multiple names.
- Whitespace is removed and the following alphabetic character is capitalized.
- Nonalphanumeric, non-underscore characters are replaced by their hexadecimal code.
- Names that do not start with a letter are prefixed with
x, and MATLAB keywords are repaired with the legacyxKeywordform such asxFor. - Names are truncated to the current
namelengthmaxcompatibility length. - Duplicate generated names and names present in
exclusionsreceive numeric suffixes.
Examples
Generate Unique Names
v = genvarname({'A','A','A','A'})Expected output:
v = {'A','A1','A2','A3'}Avoid Excluded Names
v = genvarname('A', {'A','A1'})Expected output:
v = 'A2'Normalize Invalid Text
v = genvarname('1 a-b')Expected output:
v = 'x1A0x2Db'Using genvarname with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how genvarname changes the result.
Run a small genvarname example, explain the result, then change one input and compare the output.
FAQ
Is genvarname a modern MATLAB API?⌄
No. MATLAB marks genvarname for removal and recommends matlab.lang.makeValidName with matlab.lang.makeUniqueStrings. RunMat supports it for backward compatibility with older MATLAB code.
Does genvarname create variables?⌄
No. It only returns text that can be used as variable names.
Are exclusions normalized first?⌄
No. Exclusions are treated as already-specified names to avoid, matching the documented role of excluding existing variable names.
Related Strings functions
Core
blanks · char · compose · convertCharsToStrings · convertContainedStringsToChars · convertStringsToChars · int2str · isletter · isspace · isStringScalar · isstrprop · mat2str · native2unicode · newline · num2str · sprintf · sscanf · str2double · str2num · strcmp · strcmpi · string · string.empty · strings · strlength · strncmp · strncmpi · strtok · unicode2native
Text Analytics
addDependencyDetails · addEntityDetails · addLemmaDetails · addPartOfSpeechDetails · addSentenceDetails · addTypeDetails · bagOfNgrams · bagOfWords · cosineSimilarity · doc2sequence · encode · extractFileText · extractHTMLText · fastTextWordEmbedding · findElement · getAttribute · htmlTree · ind2word · isVocabularyWord · normalizeWords · readWordEmbedding · removeLongWords · removeShortWords · removeStopWords · removeWords · stopWords · tokenDetails · tokenizedDocument · trainWordEmbedding · vaderSentimentScores · vec2word · word2ind · word2vec · wordEncoding · writeWordEmbedding
Transform
append · deblank · erase · eraseBetween · erasePunctuation · eraseURLs · extractAfter · extractBefore · extractBetween · insertAfter · insertBefore · join · lower · pad · replace · replaceBetween · reverse · split · splitlines · strcat · strip · strjoin · strjust · strrep · strsplit · strtrim · upper
Search
contains · endsWith · matches · startsWith · strfind
Pattern
digitsPattern · lettersPattern · pattern · regexpPattern · textBoundary · wildcardPattern
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how genvarname is executed, line by line, in Rust.
- View the source for genvarname in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.