I just wrote a recursive function which create a structure nested by the fieldnames given by an array but it doesn't work

if true
% code
function addfield = add(var,fields)
C = {[]};
if numel(fields)>0
k = fields{1};
field = {k};
var = cell2struct(C,field,2);
fields(1)=[];
addfield = var.(k);
add((var.(k)),fields) ;
end
addfield = var;
end end
my idea is to create the structure var and to initiate var.field1.field2.field3...fieldn= [] with fields = {fields1,field2,...,fieldn}

Asked:

on 22 Jan 2018

Answered:

on 9 Feb 2023

Community Treasure Hunt

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

Start Hunting!