-1

Let's say I have a cell c_infos <1x313 cell>, that contains (among others) a name associated to a node_ID. c_infos{1,i}.node_ID and c_infos{1,i}.name. I want to create a matrix that for each node_ID, we do have all the associated names.

I started with:

function [] = displayNodeCusts (c_infos)
list = [] ;
for i = 1:length(c_infos)
    for j = 1:length(c_infos)
        if c_infos{1,i}.node_ID == c_infos{1,j}.node_ID
            list(end+1) = c_infos{1,j}.name
        end
    end
end
end

But then I don't know which node is associated to the names.

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
ssjss7
  • 1
  • 4
  • If each `c_infos{1,i}` is a struct with the same fields, use a struct array instead of a cell array of structs. `s_infos(i).node_ID` and `s_infos(i).name`. This will be a lot more efficient in storage, and also more convenient to work with. Even better is to use `table`. You should use `cell` only for heterogeneous collections or objects that cannot be arrayed by themselves. – Cris Luengo Mar 17 '18 at 02:23

1 Answers1

0

You can use a function called cellfun which will perform the same task in all of the cells within your variable. By doing this, you can pull out all of the names without having to use for loops. I wrote some code as an example. Hope this helps.

listofnames = cellfun(@(x) x.name,c_infos,'UniformOutput',false);

uniquefromlist = unique(listofnames);

for aa = 1:length(uniquefromlist)
    allrelevantentries = strcmp(uniquefromlist{aa},listofnames);
    storage{aa} = {c_infos{allrelevantentries}};
end
Hojo.Timberwolf
  • 985
  • 1
  • 11
  • 32