can I use variable to access different structures
이전 댓글 표시
I have two structures with same field. One structure is called alum; the other is called piezo. I am assigning from these two structures doing the following:
for n=1:N
if n==1; Q0{n}=alum.Q; theta{n}=alum.th; end
if n==2; Q0{n}=piezo.Q; theta{n}=piezo.th; end
end
======================= Question: can I do this by defining a label
lbl={'alum' 'piezo'}
and then using the label in a loop to access structure alum when n=1 and structure piezo when n=2?
Thank you.
댓글 수: 2
James Tursa
2018년 10월 1일
Why is there a loop? Could N be larger than 2?
"can I do this by defining a label... then using the label in a loop to access structure..."
It is certainly possible to do that, if you want to force yourself into writing slow, complex, buggy code that is hard to debug. Read this to know why:
You could either use an if or switch to select them, or put the data into one array and use highly efficient indexing, or use a non-scalar array, or a table, or ...
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 MATLAB Support Package for Raspberry Pi Hardware에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!