Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reading a .csv file

637 views
Skip to first unread message

Ciara

unread,
Jul 9, 2014, 6:27:10 AM7/9/14
to
Hello,

I know this is a very simple question, but googling it really hasn't helped, and I've tried many different things. I have a .csv file that contains 72 numerical values in one column, and nothing else. I am using:
precipitation=dlmread('C://Users/sophysics/Desktop/annual.xlsx','\t')

but getting the error:
Error using dlmread (line 139)
Mismatch between file and format string.
Trouble reading number from file (row 1u, field 1u) ==> PK

I know that 'Mismatch between file and format string.' means that matlab isn't recognising the data as numeric, but the data is numeric.

I have also tried:
precipitation=csvread('C://Users/sophysics/Desktop/annual.xlsx','\t')

From which I get the error:
Error using dlmread (line 139)
Header lines must be integer-valued.

Error in csvread (line 48)
m=dlmread(filename, ',', r, c);

The header is integer-valued, as I have deliberately added an integer value header.

And I have tried not explicitly supplying the delimiter, and this:

precipitation=dlmread('C://Users/sophysics/Desktop/annual.xlsx',',')
precipitation=csvread('C://Users/sophysics/Desktop/annual.xlsx',',')

The data came from another .csv file that had headers, so I copied and pasted the data I need out of that file. The format is like this:
1421.17
1521.91
...
etc, up to 72 values.

Again I know this is a simple question, and you may think it is silly to be asking it, but I just can't figure this out.

Any help appreciated,
Ciara

dpb

unread,
Jul 9, 2014, 9:14:57 AM7/9/14
to
On 7/9/2014 5:27 AM, Ciara wrote:
> Hello,
>
> I know this is a very simple question, but googling it really hasn't
> helped, and I've tried many different things. I have a .csv file that
> contains 72 numerical values in one column, and nothing else. I am
> using:
> precipitation=dlmread('C://Users/sophysics/Desktop/annual.xlsx','\t')
>
> but getting the error:
> Error using dlmread (line 139)
> Mismatch between file and format string.
> Trouble reading number from file (row 1u, field 1u) ==> PK
...

According to the file extension, it isn't a .csv file at all but an
Excel file. That looks extremely likely to be the case given the error
that shows the ASCII string "PK" that was found; not a numeric value.

Try

precip=xlsread('C://Users/sophysics/Desktop/annual.xlsx');

instead. BTW, why would you try a delimiter of \t for a comma-separated
file, even if it were .csv instead of .xls(x)?

--

Ciara

unread,
Jul 9, 2014, 10:07:11 AM7/9/14
to
dpb <no...@non.net> wrote in message <lpjf7h$cm3$1...@speranza.aioe.org>...
Thank you! I'll try that now. I didn't have a good reason, apart from desperation, for trying \t. I just thought that there might be an offchance that it would work. I tried many other silly things too, that I didn't include!

Ciara

Rick Spada

unread,
Jul 11, 2014, 9:12:22 AM7/11/14
to
Hi Ciara,

Have you tried the Import Tool to import the data? Press the Import Data
button on the toolbar and use the file selection dialog to open it in the
Import Tool.

-- Rick

"Ciara " <cohar...@hotmail.com> wrote in message
news:lpj5du$pev$1...@newscl01ah.mathworks.com...

Mark Eldridge

unread,
May 5, 2017, 7:12:07 PM5/5/17
to
Ciara,

Excel (v15.30) allows you to save files with different formats that both have the suffix '.csv'.

If you save as "CSV UTF-8 (comma delimited)(.csv)", and try to load the data using csvread or dlmread, then matlab will return an error similar to the below:

----------------
Error using dlmread (line 139)
Mismatch between file and format string.
Trouble reading number from file (row 1u, field 1u) ==>
----------------

You need to save using the option "Comma Separated Values (.csv)".

n.b. that it is not sufficient to re-save the UTF-8 .csv as a standard .csv, you have to return to the original .xls/.xlsx file and save from there.

Best,
Mark



"Ciara" wrote in message <lpj5du$pev$1...@newscl01ah.mathworks.com>...

Jeff Vandegrift

unread,
May 24, 2017, 4:17:08 PM5/24/17
to
Regarding using tab separated data and calling it a "CSV"... the definition of a CSV file no longer strictly matches "Comma Separated Values", and hasn't for years. Specifically space, tab, comma, or other user specified delimiters are supported in any modern "CSV" file parser.

"dpb" wrote in message <lpjf7h$cm3$1...@speranza.aioe.org>...

Kaiwalya Swami

unread,
Jun 1, 2017, 3:36:08 PM6/1/17
to
"Mark Eldridge" wrote in message <oej0s3$cu6$1...@newscl01ah.mathworks.com>...
I have some .csv files that I am trying too read.
What happens is csvread() is able to read some of those files and others are not read correctly with error:

Mismatch between file and format string.
Trouble reading number from file (row 3200001u, field 1u) ==>

The file has only 3200000 entries.
I guess for some reason csvread() is not able to know the end of file maybe?

The weird thing is all files are printed using same function, but I am getting errors only on a few files.
Specifying the row and cols to be read is tedious as the file sizes change. I can't save the file in excel as suggested because excel will only save partial data. (i.e. max col size for excel).
Any ideas how to rectify this?
0 new messages