필터 지우기
필터 지우기

Refer to structure names

조회 수: 3 (최근 30일)
Joanna Przeworska
Joanna Przeworska 2021년 3월 30일
댓글: Joanna Przeworska 2021년 4월 1일
Hi,
Suppose I have a structure A with a, b, c, d tables inside. My aim is to use names of tables within structure A (a, b, c, d) to create new tables in structure B. I tied to do this in the following way, however it does not work. Any ideas how to corect it?
B.(fieldnames(A)'){1} = table1
B.(fieldnames(A)'){2} = table2
....

채택된 답변

Stephen23
Stephen23 2021년 3월 30일
편집: Stephen23 2021년 3월 30일
  댓글 수: 6
Stephen23
Stephen23 2021년 4월 1일
편집: Stephen23 2021년 4월 1일
"Could you explain why it is not recommended?"
Because it is more complex. More complex code is more difficult to understand, more difficult to maintain, obfuscates the code intent, is more liable to bugs, and is more difficult to debug. It is also likely to be less efficient, as it requires multiple calls to fieldnames.
Joanna Przeworska
Joanna Przeworska 2021년 4월 1일
I see your point. Thank you for your help and clarifications.
Best regards, JP

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

추가 답변 (2개)

Matt J
Matt J 2021년 3월 30일
args = [fieldnames(A).'; {table1, table2, table3,...}]
B=struct(args{:})
  댓글 수: 1
Joanna Przeworska
Joanna Przeworska 2021년 3월 31일
Dear Matt,
Thank you for your response, whowever this is not exactly what I wanted to receive.

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


Image Analyst
Image Analyst 2021년 3월 30일
"Suppose I have a structure A with a, b, c, d tables inside. My aim is to use names of tables within structure A (a, b, c, d) to create new tables in structure B."
So, why does this not work?
B = A;
New structure B will have all the same fields (a, b, c, and d) as existing structure A with the same names. Each field in the structure will have the same table as in A, which is what it seemed like you were trying to do when you assigned the tables. This also means that all the columns names of the tables of structure B will match the column names from the tables of structure A.
  댓글 수: 1
Joanna Przeworska
Joanna Przeworska 2021년 3월 31일
In my exercise, my goal is to dynamically refer to the field names of structure A in order to store the calculation in a different structure B, but with a name taken from structure A.
I don't know if I made it clear what I mean.

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

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by