Borrar filtros
Borrar filtros

Appropriate imput for dynamic structure field name

5 visualizaciones (últimos 30 días)
Jessica Angulo Capel
Jessica Angulo Capel el 22 de Abr. de 2020
Comentada: Steven Lord el 22 de Abr. de 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!!!

Respuesta aceptada

Rik
Rik el 22 de Abr. de 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 comentarios
Jessica Angulo Capel
Jessica Angulo Capel el 22 de Abr. de 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 el 22 de Abr. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Cell Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by