The error is because, in the code provided, the type of 'elementsMatrix' would be composite, mostly 'struct' (because 'elements' has nested fields in it suggesting it to be a structure (or an object)), which is not supported by 'csvwrite' as it expects it's argument types to be numeric, char or logical.
You can flatten 'elements' such that each leaf fieldname can be represented as a column in csv.
For example, subfields 'f' and 'g' of 'field1' of a structure 'S' can be represented as 'S_field1_f ' and 'S_field1_g' as follows:
A table can then be created with the flattened fieldnames as columns and the same can be written to a csv file.
As per the latest MathWorks documentation, 'csvwrite' is not recommended and instead, 'writematrix' or 'writetable' is to be used.
Thanks!