Looping through an array of strings

Hello, I have an array of trial names (i.e. trials = {'Standing', 'Walking'}, etc.) and I want to loop through the array to create new variables like so:
for trialnum = 1:11;
trial = trials(trialnum)
acc_(trial) = data.sub.(trial).acceleration
end

Respuestas (1)

Sahithi Metpalli
Sahithi Metpalli el 9 de Mzo. de 2020
Hi,
You can use the code below
trials = {'Standing','Walking'}
for trialnum = 1:length(trials)
trial = trials{trialnum}
eval(['acc_' trial '= data.sub.(trial).acceleration'])
end
But it is not recommended to name variables dynamically

1 comentario

Stephen23
Stephen23 el 9 de Mzo. de 2020
"But it is not recommended to name variables dynamically "
Then perhaps the OP might appreciate some examples of simpler, more efficeint, more reliable code?

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 4 de Mzo. de 2020

Comentada:

el 9 de Mzo. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by