matlabindexingfield-names

Matlab Fieldnames indexing


So I have a cell array with multiple tables and I am trying to access the first column name of the table.

c={'table1', 'table2', 'table3'}

Both of the following lines gives me the error:

fieldnames(c{1})(1) 
fieldnames(c{1}){1}

Error: ()-indexing must appear last in an index expression.

But if I do following, it works:

fn=fieldnames(c{1})
fn{1}

Is there a way to do this with one line of code and can someone please explain the error?


Solution

  • See this question.

    Generally such problems can be solved using a function call (either a dummy function that just returns the input) or you could just replace the fn{} with an explicit call to subsref:

    subsref(fieldnames(c{1}),substruct('{}',{1}));
    

    Regarding your question about why the error happens - maybe this link could help.