Thursday, November 28, 2013

Merging or joining cell arrays in matlab.

Array operators in Matlab...I cannot figure them out with cells. You get all sorts of funny behavior. What I wanted to do was JOIN two cell structures, so if you had: x.a=1, x.b=2, and then y.c='dogfish', you could get z.a=1, z.b=2, z.c='dogfish'. I tried about everything, but this is what I ended up with:
function result=merge_cells(p_cells1, p_cells2)
    a_names =fieldnames(p_cells1);
    for i=1:length(a_names)
        str_fieldname=a_names{i};
        str_fieldval =  p_cells1.(genvarname(str_fieldname));
        data.(genvarname(str_fieldname)) = str_fieldval;
    end
    a_names =fieldnames(p_cells2);
    for i=1:length(a_names)
        str_fieldname=a_names{i};
        str_fieldval =  p_cells2.(genvarname(str_fieldname));
        data.(genvarname(str_fieldname)) = str_fieldval;
    end
    
   result =data;
end
I dynamically make cells by reading Excel XLS files, and then I merge the sheets with this function, and then I export them back to XLS.

No comments:

Post a Comment