Looping through an array of strings

조회 수: 255 (최근 30일)
Jordyn Scism
Jordyn Scism 2020년 3월 4일
댓글: Stephen23 2020년 3월 9일
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

답변 (1개)

Sahithi Metpalli
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
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 CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by