Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Create a new array of structures from another array of structures just keeping some fields

1 回表示 (過去 30 日間)
Miguel
Miguel 2016 年 1 月 28 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Let's assume I have an array of N structures called array with fields a, b and c. I was wondering if there is any closed form of creating a new array of structures (lets called it newArray) from the original array but with only some of its fields (e.g. a and b). I think this can be a quite common task and I would be surprised if there were no close form of performing it in Matlab.
I have found two ways of performing this:
  1. Copying field by field like:[newArray.a] = array.a;[newArray.b] = array.b;
  2. Copying the whole array of structures and removing the unwanted fields:newArray = array;newArray = rmfields(newArray,'c')
However, none of them looks elegant to me. I think there might be a way of copying the array indicating the fields you want to keep.
Any help on this? thanks in advance!

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 1 月 28 日
Some people would use struct2cell() and extract portions of the result and then cell2struct() that. But copying by field would be faster.
  1 件のコメント
Miguel
Miguel 2016 年 1 月 28 日
Many thanks for the answer. Still I am surprised that there is no function for performing this.
By the way, I have a related question on this (I think this is an easier one). Imagine the data of field a in the original array were angular values expressed in degrees but in the new array I want them to be in radians. How can do this?
If I try it by running [newArray.a] = pi/180*array.a I get a "Too many arguments"error for * operation. Any advice on this?
Thanks!

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by