Effacer les filtres
Effacer les filtres

Appropriate imput for dynamic structure field name

5 vues (au cours des 30 derniers jours)
Jessica Angulo Capel
Jessica Angulo Capel le 22 Avr 2020
Commenté : Steven Lord le 22 Avr 2020
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!!!

Réponse acceptée

Rik
Rik le 22 Avr 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 commentaires
Jessica Angulo Capel
Jessica Angulo Capel le 22 Avr 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 le 22 Avr 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by