フィルターのクリア

csv file with a whitespace before comma

7 ビュー (過去 30 日間)
Bhaskar Edara
Bhaskar Edara 2015 年 9 月 15 日
コメント済み: Walter Roberson 2015 年 9 月 16 日
Hello, I have a CSV file that has a whitespace before comma and when I use csvread it adds more columns of data than it has. For example, following are the contents of the csv file.
11 ,22 ,33
If use csvread the output looks like this
>> csvread('test.csv')
ans =
11 22 33 0 0
Is there any way around this other than removing the white-spaces before csvread call?
Thank you in advance, Bhaskar
  2 件のコメント
per isakson
per isakson 2015 年 9 月 15 日
編集済み: per isakson 2015 年 9 月 15 日
I cannot reproduce your result on R2013a
>> csvread('cssm.csv')
ans =
11 22 33
>> version
ans =
8.1.0.604 (R2013a)
Please, upload your csv-file with the help of the paper-clip-button
Bhaskar Edara
Bhaskar Edara 2015 年 9 月 16 日
This seems to be an issue with 2015a. It works fine in 2014a for me.

サインインしてコメントする。

回答 (3 件)

Kirby Fears
Kirby Fears 2015 年 9 月 15 日
I reproduced the error in 2015a. Indeed there are two trailing 0 columns when the csv file contains white space, but no trailing columns after deleting the whitespace.
I recommend using delimread . Download the function, add it to your path, then read your file using:
num = delimread('test.csv', ',' ,'num');
  5 件のコメント
Kirby Fears
Kirby Fears 2015 年 9 月 16 日
編集済み: Kirby Fears 2015 年 9 月 16 日
You can report this as a bug; it may get fixed eventually. The built-in csvread and dlmread functions both have this bug that you've pointed out. They also have very limited functionality, so it may be in your best interest to switch to a new function now instead of putting it off.
I think Mathworks is pushing users to adopt tables and readtable() and therefore have neglected these old csv reading functions. So switching to readtable seems like the smart approach if you're cool with dealing in tables instead of simple arrays.
I made the delimread function (linked above) when I ran into the same problem you're facing now. It reads much more flexible input types but sticks with simple array (or cell) outputs.
Walter Roberson
Walter Roberson 2015 年 9 月 16 日
csvread() has historically called dlmread() to do the work. dlmread() has historically called textscan() to do its work. textscan() is being actively maintained -- it was, for example, enhanced to handle date formats.

サインインしてコメントする。


Jae Song
Jae Song 2015 年 9 月 15 日
How about using the offset parameters of the csvread function. For your example:
csvread('test.csv',0,0,[0,0,0,2])
  1 件のコメント
Bhaskar Edara
Bhaskar Edara 2015 年 9 月 16 日
with out the knowledge of the file offset is difficult to use though.

サインインしてコメントする。


Brendan Hamm
Brendan Hamm 2015 年 9 月 15 日
編集済み: Brendan Hamm 2015 年 9 月 15 日
You can also use xlsread or readtable (the latter requires version 2013b or later).
xlsread('test.csv')
readtable('test.csv','ReadVariableNames',false)
  2 件のコメント
Bhaskar Edara
Bhaskar Edara 2015 年 9 月 16 日
Thank you. Unfortunately this is an existing code that seems to work fine for some users and it is a pain to go and change all the code but will keep these options in mind for new development.
Kirby Fears
Kirby Fears 2015 年 9 月 16 日
It's worth noting that xlsread should not be used on files that are not explicitly excel-formatted files (.xls, .xlsx, .xlsm, etc) since it will fail to read .csv files in non-Windows operating systems. The xlsread function also has way more overhead than simple text reading functions. It is extremely slow for large csv files.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by