Add a column to a structure
31 ビュー (過去 30 日間)
古いコメントを表示
Hi i am a beginner with matlab and i have issues while dealing with structure
I have the following structure
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1534552/image.png)
each row looks like this
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1534557/image.png)
i want to add a each column inside the field distance, a column with value stored in an arrey.
I wrote this but it didn t work, i think the column newstruct must go inside the field distance but how to do that?
newstruct = 'ACTION'
DISTANZA(1).newstruct = [];
DISTANZA(2).newstruct = [];
DISTANZA(3).newstruct = [];
DISTANZA(4).newstruct = [];
DISTANZA(5).newstruct = [];
DISTANZA(6).newstruct = [];
DISTANZA(7).newstruct = [];
DISTANZA(8).newstruct = [];
DISTANZA(9).newstruct = [];
DISTANZA(10).newstruct = [];
values_cell = [1;1;0;0;1;1;0;0;1;0]
[DISTANZA.(newstruct)] = values_cell{:};
thank you for any help
elena
0 件のコメント
回答 (1 件)
Sai Teja G
2024 年 7 月 26 日
Hi Elena,
I understand that you want to add a new column named 'ACTION' to each field within your struct.
The issue you're encountering stems from using curly braces, which are specifically intended for assigning values to cell arrays. To correctly add the 'ACTION' column to your struct, you can use the following code as a reference and adapt your code accordingly:
for i = 1:numel(DISTANZA)
DISTANZA(i).(newstruct) = values_cell(i);%as you are using normal array avoid curly braces
end
Hope it helps!
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!