How to add a new field to a struct array inside a cell array
19 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Juan Herrera
am 19 Aug. 2020
Kommentiert: Juan Herrera
am 21 Aug. 2020
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
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 19 Aug. 2020
afx2 = cellfun(@(S,K) setfield(S, 'f4Orient', K), A, BnewCell);
1 Kommentar
Weitere Antworten (2)
Stephen23
am 19 Aug. 2020
Bearbeitet: Stephen23
am 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.
Bruno Luong
am 19 Aug. 2020
Bearbeitet: Bruno Luong
am 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
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!