Why am I unable to open .csv file with the readmatrix function?

The file I am trying to import in matlab is CSV (comma delimited).
So each cell in the excel file is like:
0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 3.000
So 11 values with space in between.
Initially using matlab 2020a, readmatrix could read each value separately and could import it without any issues as a 1 x 11 double.
But now I am not able to anymore. It would just read it as 1x1 double with the value as NaN.
Any suggestions as to how I can read my excel file now?

10 Comments

please attach one of the files you are having trouble with.
"So each cell in the excel file is like:"
CSV files are not Excel files. CSV files are text files that exist completly independently of Excel (or for that matter, of any other application). Excel is an extremely unreliable tool for analyzing the content of a text file (such as CSV files, or tab-separated files (like yours would be if it weren't double quoted as text)).
Use a text editor instead.
"So 11 values with space in between."
Nope. In fact the file actually contains exactly one column of double-quoted text:
Double quotes are used to indicates text values that should be treated as one value, regardless of any delimiters etc. that may occur within those quotes. Ergo, your file contains exactly one column of text.
If you expect eleven columns of numeric fields, then you need to fix the tool that wrote that file.
Is there any way to salvage this data? Or I need to manually remove the " "?
Why is it a problem? Just edit the file. Could you write specific code that would read in the file as text, then remove the quotes and then convert the text to numeric values? Yes, as that would be almost trivial. You would need to use textread then. Is this how you expect all of these files to appear now, and you get many files to read? Perhaps it is best to work at the source, and decide why it is that now you are getting files in a different form than previously.
I removed the double quotes using a simple text editor:
M = readmatrix('testfile.txt')
M = 56709×11
0 0 0 0 0 0 0 0 0 0 3.0000 0.0070 0.0330 0 0 1.0000 0 0 0 0 0 0 0.0070 0.0330 0 0 1.0000 0 0 0 0 0 0 0.0090 0.0330 0 0 1.0000 0 0 0 0 0 0 -0.0100 0.0370 0 0 1.0000 0 0 0 0 0 0 -0.0020 0.0340 0 0 1.0000 0 0 0 0 0 0 -0.0020 0.0350 0 0 1.0000 0 0 0 0 0 0 0.0010 0.0340 0 0 1.0000 0 0 0 0 0 0 0.0010 0.0340 0 0 1.0000 0 0 0 0 0 0 0.0040 0.0330 0 0 1.0000 0 0 0 0 0 0
Fiddling with the READMATRIX options is certainly possible, but fixing the source of the problem is better.
@John D'Errico that is a good suggestion. I will try to use your idea of textread and convert to numerical values eventually.
No the new files from source would not have this problem. I fixed that issue but I wanted to still solve this problem as all the cells in some files have double quotes and they cannot be redone.
@Stephen23 may I ask which text editor app you used?
@Poulomi: I used https://notepad-plus-plus.org/downloads/, but every text editor on the planet has find-and-replace functionality.
@Stephen23 thank you!! find and replace function at notepad++ solved my problem... matlab can read the csv file now.

Sign in to comment.

 Accepted Answer

hello
workaround suggestions
faster with readcell vs importdata
>> tic;
out2 = str2double(split(readcell('t4md6p1 - partial file.csv')));
toc
Elapsed time is 10.480521 seconds.
>> tic;
out2 = str2double(split(importdata('t4md6p1 - partial file.csv')));
toc
Elapsed time is 34.013224 seconds.

More Answers (0)

Products

Release

R2020a

Asked:

on 10 Jun 2022

Commented:

on 13 Jun 2022

Community Treasure Hunt

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

Start Hunting!