MATLAB invalid field name

282 ビュー (過去 30 日間)
Paolo
Paolo 2017 年 6 月 8 日
回答済み: Steven Lord 2017 年 6 月 8 日
Hello, I am trying to create the following structure
data2.TEST.('MY/FIELD')=100
receiving the following error
Invalid field name: 'MY/FIELD'
I guess the error is related to the use of "/" character in the field name. Any suggestion about how to solve this and maintain the field name with "/" ?
thank you P

採用された回答

Steven Lord
Steven Lord 2017 年 6 月 8 日
Field names in MATLAB struct arrays must satisfy three of the four criteria of a valid variable name as listed in the help for the isvarname function. They must:
  1. start with a letter, otherwise assigning to that field will error
  2. contain only letters, numbers, and/or the underscore character, otherwise assigning to that field will error, and
  3. must be no longer than namelengthmax (currently 63) characters, otherwise you will receive a warning and the field name will be truncated
It is possible to assign to a field whose name is a keyword, though I would advise against it unless absolutely necessary. And trying to save that struct with the -struct option probably won't work, or if it does trying to load that MAT-file will likely cause a headache for you in the future.
S = struct;
S.end = 42
To convert your potential field name into one that is valid, consider using matlab.lang.makeValidName and potentially matlab.lang.makeUniqueStrings.

その他の回答 (1 件)

dpb
dpb 2017 年 6 月 8 日
" how to solve this and maintain the field name with "/" ?"
No can do. The '/' is simply not an allowable character in a variable/field name and there's no getting around it. Use something else.

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by