get fieldname from structure in matlab

To get the field names from a structure in Matlab, you can use the fieldnames function. Here's an example:

main.m
        ```
        % Define a sample structure
        s = struct('field1', 123, 'field2', 'abc', 'field3', [1 2 3]);

        % Get the field names
        fields = fieldnames(s);

        % Display the field names
        for ii = 1:numel(fields)
            disp(fields{ii})
        end
        ```

        This will output:
        ```
        field1
        field2
        field3
        ```

        You can then use the `fields` variable to access the fields of the structure programmatically. For example, to get the value of the `field1`, you can do `s.(fields{1})`.
578 chars
22 lines

related categories

gistlibby LogSnag