Run Section funcionality isn't working

14 views (last 30 days)
I click on the "Run Section" button and it doesn't do anything.
It used to function correctly, but only this time when I open matlab it doesn't work.
It's nothing to do with the file name right?

Accepted Answer

Steven Lord
Steven Lord on 18 Sep 2023
In order to run MATLAB code in a code file, the name of that file must satisfy the requirements given by the isvarname function. Those requirements are:
  • The name must start with a letter (upper-case or lower-case doesn't matter.)
  • The name must contain only letters, numbers, and/or the underscore character.
  • The name must be no longer than the output of the namelengthmax function (currently 63.)
  • The name must not be a keyword in MATLAB. [This requirement can be relaxed or ignored in certain circumstances, but regardless it's not what's causing Run Section to fail to work.]
Does your name satisfy these requirements? No, it doesn't.
name = 'matlab reflection coefficients.m';
[~, nameNoExt] = fileparts(name) % Name without the extension
nameNoExt = 'matlab reflection coefficients'
isvarname(nameNoExt)
ans = logical
0
This name satisfies the first requirement:
isletter(nameNoExt(1))
ans = logical
1
the third requirement:
strlength(nameNoExt) <= namelengthmax
ans = logical
1
and the fourth requirement:
~iskeyword(nameNoExt)
ans = logical
1
It fails the second.
isUnderscore = nameNoExt == "_";
isLetterOrDigit = isstrprop(nameNoExt, 'alphanum'); % letters or digits
letter_digit_or_underscore = isLetterOrDigit | isUnderscore;
all(letter_digit_or_underscore)
ans = logical
0
Which characters are not letters, digits, or underscores? The spaces.
invalidCharacters = nameNoExt(~letter_digit_or_underscore)
invalidCharacters = ' '
Rename your code file to a name that satisfies all the requirements. You could replace the spaces with the underscore character, or you could remove the spaces (perhaps capitalizing the first letter of each word after the first.) The matlab.lang.makeValidName function can suggest a valid name similar to the name you specified.
newname = matlab.lang.makeValidName(nameNoExt)
newname = 'matlabReflectionCoefficients'
isvarname(newname)
ans = logical
1

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!