Appropriate imput for dynamic structure field name

5 views (last 30 days)
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!!!

Accepted Answer

Rik
Rik on 22 Apr 2020
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 Comments
Jessica Angulo Capel
Jessica Angulo Capel on 22 Apr 2020
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 on 22 Apr 2020
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!