Looping through an array of strings
248 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Sahithi Metpalli
2020-3-9
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 个评论
Stephen23
2020-3-9
"But it is not recommended to name variables dynamically "
Then perhaps the OP might appreciate some examples of simpler, more efficeint, more reliable code?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!