How to add a new field to a struct array inside a cell array

39 views (last 30 days)
Hi all :)
I'm trying to add a new field to every structure contained withing a cell array myTLS {1x300}, using anonymous Functions, that is,with cellfun / arrayfun.
The structure it self has the same Fields / Fieldnames in every cell. Only the content is different.
It looks something like this, for example for the 2 first cell elements.
A{1, 1}.f1trajectory = 1:10
A{1, 1}.f2TLSid = 7
A{1, 1}.f3FZklasse = "2.PkwA"
A{1, 2}.f1trajectory = 2:11
A{1, 2}.f2TLSid = 3
A{1, 2}.f3FZklasse = "3.LkwA"
Now I have another cell array: BnewCell: {1x300}, and inside each a double [1x1]. The value of each cell corresponds to each vehicle from myTLS {1x300}.
How could I add a new 4th. field ‘f4Orient’ for all the structures, that is, for the structure inside every of the 300 Cell elements? Preferably using anonymous function, that is, cellfun or arrayfun.
It should look like something like this:
BnewCell = {5 ,12,17,19} % Orientation values
A{1, 1}.f4Orient = BnewCell{1,1}
A{1, 2}.f4Orient = BnewCell{1,2}
I’m having trouble formulating an Anonymous Function, which I could use together with cellfun or arrayfun, to accomplish this inside my cell array myTLS.
af1x = @(x) x % get contents of BnewCell
C = cellfun (af1x,BnewCell,'UniformOutput',false)
I though about something like this, but it doesn't work when using twice “=“
afx2 = @(x) x(:).f4Orient = @af1x(x); % get error, cant use twice "="
I’d greatly appreciate your help.
Thanks in advance

Accepted Answer

Walter Roberson
Walter Roberson on 19 Aug 2020
afx2 = cellfun(@(S,K) setfield(S, 'f4Orient', K), A, BnewCell);
  1 Comment
Juan Herrera
Juan Herrera on 21 Aug 2020
Thanks a lot for your answer Walter, I just got try it, and worked perfectly.
Would it be possible, as well with some sort of cellfun / anonym. function to add at the same time multiple fields?
Let's say my BnewCell is now {7x300} and I would like to add each Row as a new different Field. Using your suggestion, that is the setfield command, I'm only able to add one row at a time.
% Error: input cells have different dimensions -> A{1x303} - BnewC {3x303}
afx2 = cellfun(@(S,K) setfield(S, 'f4Orient', K), A, BnewCell);
% Add first row of BnewCell
afx3 = cellfun(@(S,K) setfield(S, 'f4Orient', K), A, BnewCell(1,:));
Thanks again.

Sign in to comment.

More Answers (2)

Stephen23
Stephen23 on 19 Aug 2020
Edited: Stephen23 on 19 Aug 2020
Rather than a cell array of scalar structures (difficult to work with) you should use one structure array (easier to access):
A(1).f1trajectory = 1:10;
A(1).f2TLSid = 7;
A(1).f3FZklasse = "2.PkwA";
A(2).f1trajectory = 2:11;
A(2).f2TLSid = 3;
A(2).f3FZklasse = "3.LkwA";
Then all you need to do is this:
[A.f4Orient] = BnewCell{:};
Read about how this works:
The other answers you will get will use cellfun or other inefficient syntaxes: none will be as simple or efficient as designing your data better and using one comma-separated list.
  1 Comment
Juan Herrera
Juan Herrera on 21 Aug 2020
Thanks a lot for your answer and the links Stephen! Will take a look at your suggestion.

Sign in to comment.


Bruno Luong
Bruno Luong on 19 Aug 2020
Edited: Bruno Luong on 19 Aug 2020
If your structures has the same fiednames then it is better to put them in structure array (s in the code below) rather than cell array of structure (c in the code).
>> c{1}=struct('f1',1,'f2',2);
>> c{2}=struct('f1',3,'f2',4);
>> f3val={5 6};
>> s=cell2mat(c) % convert to structure array
s =
1×2 struct array with fields:
f1
f2
>> [s.f3]=deal(f3val{:}); % command to add the field
>> s
s =
1×2 struct array with fields:
f1
f2
f3
>> s(1)
ans =
struct with fields:
f1: 1
f2: 2
f3: 5
>> s(2)
ans =
struct with fields:
f1: 3
f2: 4
f3: 6
>> c=num2cell(s) % convert back to cell array, waste time to convert back-forth and not a good design of data structure
c =
1×2 cell array
{1×1 struct} {1×1 struct}
>> c{1}
ans =
struct with fields:
f1: 1
f2: 2
f3: 5
>> c{2}
ans =
struct with fields:
f1: 3
f2: 4
f3: 6

Categories

Find more on Structures in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!