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

107 visualizaciones (últimos 30 días)
Joan Vazquez
Joan Vazquez el 1 de Sept. de 2020
Comentada: Ben el 18 de Abr. de 2024 a las 12:56
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' ?

Respuesta aceptada

Stephen23
Stephen23 el 1 de Sept. de 2020
Editada: Stephen23 el 1 de Sept. de 2020
Matlabcalifragilisticexpialidocious:
[S.name] = deal(data.name)
  2 comentarios
Joan Vazquez
Joan Vazquez el 1 de Sept. de 2020
Lovely.
Matlab's marketing department needs to popularize a proper adjective :P
Ben
Ben el 18 de Abr. de 2024 a las 12:56
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*

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by