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

NameTypeRequiredDefaultDescription
strAnyYesInput text, string array, character array, or cell array of text scalars.
exclusionsAnyNoNames that generated output must not match.

Returns

NameTypeDescription
varnameAnyValid variable name text with the same container form as the input.

Errors

IdentifierWhenMessage
RunMat:genvarname:InvalidInputInput is not text-compatible or cell elements are not text scalars.genvarname: input must be text or a cell array of text
RunMat:genvarname:InvalidOptionMore than one exclusions argument is supplied.genvarname: expected str or str,exclusions
RunMat:genvarname:InternalInternal 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 legacy xKeyword form such as xFor.
  • Names are truncated to the current namelengthmax compatibility length.
  • Duplicate generated names and names present in exclusions receive 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.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how genvarname is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.