Filter löschen
Filter löschen

Looping through an array of strings

294 Ansichten (letzte 30 Tage)
Jordyn Scism
Jordyn Scism am 4 Mär. 2020
Kommentiert: Stephen23 am 9 Mär. 2020
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

Antworten (1)

Sahithi Metpalli
Sahithi Metpalli am 9 Mär. 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 Kommentar
Stephen23
Stephen23 am 9 Mär. 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?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by