Why am I unable to open .csv file with the readmatrix function?
Show older comments
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
Walter Roberson
on 10 Jun 2022
please attach one of the files you are having trouble with.
Poulomi
on 10 Jun 2022
"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.
Poulomi
on 10 Jun 2022
John D'Errico
on 10 Jun 2022
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')
Fiddling with the READMATRIX options is certainly possible, but fixing the source of the problem is better.
Poulomi
on 12 Jun 2022
Poulomi
on 12 Jun 2022
Stephen23
on 12 Jun 2022
@Poulomi: I used https://notepad-plus-plus.org/downloads/, but every text editor on the planet has find-and-replace functionality.
Poulomi
on 13 Jun 2022
Accepted Answer
More Answers (0)
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!