Run Section funcionality isn't working

15 ビュー (過去 30 日間)
Shaoming
Shaoming 2023 年 9 月 18 日
回答済み: Steven Lord 2023 年 9 月 18 日
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?
  1 件のコメント
Mario Malic
Mario Malic 2023 年 9 月 18 日
It says what the error is. Invalid MATLAB file name.

サインインしてコメントする。

採用された回答

Steven Lord
Steven Lord 2023 年 9 月 18 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by