How to (nicely) copy a structure array field into another

Based on several questions in this forum, the following piece of code works. But I wonder if there is a more 'elegant' or 'neat' way in Matlab ('matlabish', 'matlabonic'...?)
% S is a given structure (I definetely can't modify that)
S(1).name = [];
S(2).name = [];
% Nicely stored input data (I could parse this differently, but this is not the point)
data(1).name = 'A';
data(2).name = 'B';
% Is there a more straightforward way to achieve 'S.name = data.name' ?
names = {data.name};
[S.name] = names{:};
% Or a clean one liner that avoids using 'names' ?

 채택된 답변

Stephen23
Stephen23 2020년 9월 1일
편집: Stephen23 2020년 9월 1일
Matlabcalifragilisticexpialidocious:
[S.name] = deal(data.name)

댓글 수: 2

Lovely.
Matlab's marketing department needs to popularize a proper adjective :P
After seeing many arrayfuns/loops/etc. used as solutions for similar questions on this site, along with getting them suggested by LLM chat bots, this solution is just so satisfying
*feels good meme*

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

추가 답변 (0개)

카테고리

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

제품

릴리스

R2019a

질문:

2020년 9월 1일

댓글:

Ben
2024년 4월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by