Add a column to a structure

조회 수: 26 (최근 30일)
Elena
Elena 2023년 11월 9일
답변: Sai Teja G 2024년 7월 26일
Hi i am a beginner with matlab and i have issues while dealing with structure
I have the following structure
each row looks like this
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

답변 (1개)

Sai Teja G
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!

Community Treasure Hunt

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

Start Hunting!