MATLAB: Accesing structure using a function

functionMATLABstructures

Dear all,
I have a function that needs to treat data coming from a structure, and I want to have several inputs indicating which branch of the structure is the one to be treated:
function total = totalCampus(campus,building,year,var)
total=getfield(campus,(year),(building),(var))
end
This is just the first line of the function, where I want to call the specific column of the table in the structure, using dynamic fieldnames, but I can't manage. Any clue?
best regards,
Mario

Best Answer

  • total = campus.( building ).( year ).( var );
    should work fine, assuming campus is your struct and that is a branch of the struct.