Hello,
Is it possible to create a structure in a for loop
field={'name1','name2'}
for i=1:2
structure.(field{i})=value
end
Output:
structure.name1=value
structure.name2=value
Thanks
Jinang

댓글 수: 2

Ruger28
Ruger28 2019년 11월 22일
Please, use the code format.
What are you having issues with? This will work just fine.
jinang patel
jinang patel 2019년 11월 22일
hey,
a={'see','why'}
KPI={'L','L2','L3'}
struc.a{1}.KPI{1}=5
Output
"struc =
struct with fields:
a: {[1×1 struct]}"
I would expect
struct.see.L=5

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
Stephen23 2019년 11월 22일

3 개 추천

Use the syntax shown in the MATLAB documentation:
>> a = {'see','why'};
>> KPI = {'L','L2','L3'};
>> S.(a{1}).(KPI{1}) = 5;
>> S.see.L
ans = 5
Read more:

댓글 수: 2

jinang patel
jinang patel 2020년 4월 29일
hello Stephen,
I am trying to assing value to each field name, however this is not possible since assign value to 1st field name limits the structure. Is there a way to assign value to individual fields?
a = {'see','why'};
KPI = {'L','L2','L3'};
S.(a{1})=6;
S.(a{1}).(KPI{1}) = 5;
Output
S.see=6;
S.see.L=5;
Stephen23
Stephen23 2020년 4월 29일
편집: Stephen23 2020년 4월 30일
That is not possible. A field contains one array, i.e. the field can contain either a numeric array or another structure array, but it cannot simultaneously contain both.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

질문:

2019년 11월 22일

편집:

2020년 4월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by