csvwrite error: Undefined function 'real'

9 views (last 30 days)
Donovan Magney
Donovan Magney on 11 Apr 2018
Answered: Rajanya on 11 Mar 2025
I wrote a code to ask the user for information on the periodic table and put it into a struct. That all worked just fine. But I now need to write it to a cvs file and thats where it is not working. Im getting the error: "Underfind function 'real' for input arguments of type 'struct'".
The code where the error(s) are occuring
Im not too familiar with cvs. So am I not doing it right or am I hitting an error because of the way I wrote the code and how can I fix it?

Answers (1)

Rajanya
Rajanya on 11 Mar 2025
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!

Tags

Community Treasure Hunt

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

Start Hunting!