Define a new structure from another one

13 次查看(过去 30 天)
Hi,
I have structure (STR) as struct 20x1 with two fields (A and B).
I want:
1- to call STR in a function,
2- and I want to define a new structure with a new name for example SS.P in which, it is equal to STR.A.
I know that I can use "SS.P=STR.A", but in this case, I don't want to use "A".
I'm looking for some thing like: SS.P =STR(:,1) and in this statement, 1 is the first column (field) of STR. Unfortunetely, the commend of SS.P =STR(:,1) is not a correct comment in MATLAB. Could anyone give me the right comment to do this?
  4 个评论
civil tech
civil tech 2020-11-6
The problem here is that the name of A is changed in each calling of the function. So, I can't use "SS.P=STR.A".
civil tech
civil tech 2020-11-6
Consider this:
STR : struct 25x1
fields A B
1 1X10 1X1
2 1X10 1X1
3 1X10 1X1
How I can define a new structure S (having only one field P) including the first column of STR (column A) and all rows (1,...,3), without using the name of A? without using the code of S.P=STR.A

请先登录,再进行评论。

回答(1 个)

Bruno Luong
Bruno Luong 2020-11-6
编辑:Bruno Luong 2020-11-6
fname = fieldnames(STR)
S.P = STR.(fname{1}); % S = struct('P', {STR.(fname{1})})

类别

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