Appropriate imput for dynamic structure field name

5 次查看(过去 30 天)
Hello,
I am trying to create a dynamic field name to a structe with the dot notation. I.e. I have a for loop with a string variable that changes in each iteration, and I want to create a field with that string name in each of the iterations.
msd = struct;
for i = 1:NFiles %for each file
FileName = Track_file{i,1}.File(1);
msd.(FileName) = []; %mal
end
The variable FileName is a string, but it keeps giving me an error: Invalid field name: 'Z:\Documents\Data\Sec1 vesicles\Spots in tracks statistics.csv'.
I also tried with:
msd.(FileName{1}) = [];
but the error is the same.
Why is it an invalid field name? which class of variable should I use as an imput?
Thank you very much!!!

采纳的回答

Rik
Rik 2020-4-22
Field names in matlab adhere to the same rules as variable names. They can't start with a digit and can only contain 0-9, a-z, A-Z, and underscores.
Since Z:\Documents\Data\Sec1 vesicles\Spots in tracks statistics.csv is not a valid variable name, it can't be a field name. You will have to store that file name in a different way.
  2 个评论
Jessica Angulo Capel
Thank you very much!
I was so convinced it had to be a problem with the imput variable type that I didn't think of that. So simple!
Steven Lord
Steven Lord 2020-4-22
You could try converting that variable name to a valid identifier using matlab.lang.makeValidName. Alternately if you're using release R2019b or later you could make a table with a variable name that's not a valid MATLAB identifier.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by