File Exchange

image thumbnail

csv2struct(filename​)

version 1.0.0.0 (2.12 KB) by Jaroslaw Tuszynski
reads Excel's files stored in .csv or .xls file formats and stores results as a struct

21 Downloads

Updated 22 Feb 2016

View License

CSV2STRUCT reads Excel's files stored in .xls or .csv file formats and stores results as a struct.
DESCRIPTION
The Excel file is assumed to have a single header row. The output struct will have a field for each column and the field name will be based on the column name read from the header.
Unlike csvread, csv2struct is able to read files with both text and number fields and store data as fields of a struct. Likely works on Windows machines only.

See also:
MATLAB's csvread and xlsread functions
xml_read from my xml_io_tools which creates struct out of xml files

Cite As

Jaroslaw Tuszynski (2021). csv2struct(filename) (https://www.mathworks.com/matlabcentral/fileexchange/26106-csv2struct-filename), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (19)

Morgan Waddell

Sebastian Bellitto

Jerome BlahaJr

Awesome Script and thank you Jaroslaw!

Beware that some CSV files larger than 1,048,575 rows may be truncated during import, possibly due to issues with the Matlab built-in xlsreadCOM called during CSV import (Tested in Matlab R2015b)

Zvi Marks

Charles Krzysik

Haldun

Hi Jaroslaw,

When i call this funciton within another script i get "Expression or statement incorrect-possibly unbalanced" error. The error refers to Line 33 Column 3, which corresponds to tilde operator. Do you have an idea ?

Jason Baxter

I need to transfer data structures from Matlab to C, I was hoping this would do it but this doesn't work if the structures have more than one level.

For example:

>> test.a1.a2.a4=1;
>> test.a1.a2.a2=10;
>> test.a11.a2.a1=1;
>> test.b=[12;42;12];
>> test.c={'safds','sdfs'};

Runs but produces nonsense. Please fix or list the limitations in the description and help entry.

Andrea

I keep getting the following error message when trying to run the code...

Invalid field name: '@'.

Error in csv2struct_TEST (line 82)
Out.(name) = num(:,iNum);

Any suggestions for debugging? Am I missing something?

Andrew

chang hsiung

can this be used in Mac ?

Thai

Great code.
Thanks

Michael

Awesome script! CSV only works for me if i have a header with more than one character.

Jeff Barton

Just what I was looking for. Thank you.

shlomix

it didn't work well for me when I passed a csv file which size exceeded 2^16.

Pete

@Ged: "***If your system does not have Excel for Windows installed, or MATLAB cannot access the COM server, xlsread operates in basic mode. In this mode, xlsread only reads XLS files.***"

Yeah this is unfortunate - would seem to mean that this script will only work on windows machines. Not the author's fault, but down-rated accordingly =/

Michael Chan

It's really useful.
just a minor error.

line 78: name = char('A'-1+c);
instead of
name = char('A'-1+i);

Alfred

Ged Ridgway

Really useful submission!

Works nicely on .xls, but doesn't seem to work on .csv for me... In particular, xlsread (which csv2struct relies on even for .csv) doesn't seem happy with .csv files at all; I'm not sure if this is a platform issue (xlsread under Windows can be more advanced than xlsread under Linux, which I'm currently using) or a version issue (I'm currently using R2006a). Any ideas? (It's easy enough to convert csv to xls, and then use it, but csv is more widely compatible than xls for other software...)

Pete

Have only just started playing with this script, but so far it seems to be another truly excellent utility from Jaroslaw Tuszynski.

Does everything it says. Columns with text come out as cells. Columns with numbers come out as matrices. And all of them are stored in a structure such that you can reference them by the name of the column header.

This seems to me far superior to anything else I've found either here on FEX, or built in to Matlab (though I'm only running R2007a at the moment, so I can't comment on newer releases).

Will post an update if I come across any problems further down the line, but on present experience I highly recommend this script!

MATLAB Release Compatibility
Created with R2008b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired: Struct array conversion

Community Treasure Hunt

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

Start Hunting!