I have two cell array's with structure in it.
example:
xmlFB = Columns 1 through 5 [1x1 struct] [1x1 struct] [1x1 struct] [1x1 struct] [1x1 struct]xmllink = Columns 1 through 3 [1x1 struct] [1x1 struct] [1x1 struct]
xmlFB{1} ans =
Param: {[1x1 struct] [1x1 struct]} InterChartConnection: [1x1 struct] Tasks: [1x1 struct] Attributes: [1x1 struct] xmllink{1} ans = Attributes: [1x1 struct]
In "xmllink" there is the struct "Attributes" and in there is the Field "Name" with the Value "EN1"
And in xmlFB in the struct "Attributes" there are two Fields "Name" and "Typ"
The Names are unique.
What i want to do is to find the "Typ" in "xmlFB" from the names in the "xmllink".
First i wanted to do with a Loop, but the i read about these arrayfun/structfun/cellfun functions from Matlab.
Is there a way to do this with these? Or is a Loop better?
Let's assume that all variables in Names and Typ are strings. (Should work for scalars as well.)
Here is how to get the values into cell arrays and link them.
% create cell arrays
xmllinkname = cellfun(@(x) x.Attributes.Name, xmllink, 'UniformOutput',0);
xmlFBname = cellfun(@(x) x.Attributes.Name, xmlFB, 'UniformOutput',0);
xmlFBtyp = cellfun(@(x) x.Attributes.Typ, xmlFB, 'UniformOutput',0);
% match names
[idx1, idx2] = ismember(xmllinkname,xmlFBname);
idx2(idx2==0)=[]; % in case some names in xmllink absent in xmlFB
% get matched names and typ
xmllinknamematched = xmllinkname(idx1);
xmllinktyp = xmlFBtyp(idx2);
Since the order of values in the first cell arrays will be the same as in your original cell arrays of structures, you can use those indices idx1
and idx2
to match the structures as well.
xmllinkmatched = xmllink(idx1);
xmlFBmatched = xmlFB(idx2);
Of course, you can avoid creating the temporary arrays and put the first two cellfun
statement into ismember
statement.