Importdata incorrectly reads csv file

12 views (last 30 days)
PSBclocktower
PSBclocktower on 13 Aug 2020
Answered: Cris LaPierre on 13 Aug 2020
I have a csv file that I am trying to import using the "importdata" command. In the csv file, the headers begin on row 12, and there are several columns of cells containing data, that are separated by a single blank column (ex: 5 columns of data, then a blank column, then another 5 columns of data, then another blank column, and finally another 5 columns of data).
My original code was:
headerlinesIn=12;
delimiterIn=',';
OC=importdata(OrigFile,delimiterIn,headerlinesIn)
mydata=OC.data;
---
MATLAB does not throw an error, but when I look at the variable "mydata", it is a type double array containing only last 5 columns of data, not the data from the whole csv file. Do you have any suggestions? I'm sorry I cannot provide the content of the csv file for troubleshooting.
  2 Comments
Walter Roberson
Walter Roberson on 13 Aug 2020
are any of the columns text?
have you tried using readtable instead?
dpb
dpb on 13 Aug 2020
Edited: dpb on 13 Aug 2020
" I cannot provide the content of the csv file for troubleshooting. "
Well, hard to debug what we cannot see...but importdata isn't the most user-controllable option; it's designed to be easy for the user if things are regular and simple.
If you have missing data and particularly if there are mixed data types, try using detectimportoptions first and the importdata object it creates to control what fields are recognized and how are interpreted.

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 13 Aug 2020
Try importing your data interactively using the Import Tool. There, you can set data types, data ranges, etc. This video may be helpful for learning how to use it. You can also generate the code used to import the data from the Import Tool. This video on generating and reusing code created by the import tool shows you how.

Community Treasure Hunt

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

Start Hunting!