Matlab readmatrix inconsistently reading csv files
Afficher commentaires plus anciens
I'm using matlabs readmatrix function to read in data from a csv file and store to a variable. The csv files are identical in format, with a bunch of lines of text at the start before the data starts at line 21. However, the readmatrix function seems to behave inconsistently, sometimes capturing all the text at the start of the csv and storing as NaN, and other times ignoring these first 21 lines and only grabbing the data. Why is this? What is a better way to do this?
7 commentaires
Stephen23
le 24 Août 2023
"Why is this?"
Because the files are different.
"What is a better way to do this?"
Specify the format, e.g. using
and modifying the options based on prior knowledge about the file format.
Christian Taylor
le 24 Août 2023
Steven Lord
le 24 Août 2023
Can you show small samples of two CSV files that are formatted exactly the same but are interpreted by readmatrix differently? Can you also show the exact readmatrix command you're executing that interpreted those two files differently?
"As per the question, the files are not different. "+
They are different, even if only in the data values. Click the paperclip button to upload two files, which show different behavior when imported using READMATRIX.
Also show your exact code where you import them.
Christian Taylor
le 24 Août 2023
Christian Taylor
le 24 Août 2023
"I have just opened my csv files in a text editor. Whilst the headers look identical in Excel, in the text editor there are a number of comma delimiters after most lines on one of the files. Perhaps this explains the different behaviour."
Yes, differences between the files is most likely the cause.
Of course the algorithm used by READTABLE et al is not perfect (there is no such thing) and it cannot read minds: what is obevious to a human is not obvious to a machine. It is always possible to trick or confuse an algorithm with the right combination of data or whatever, such things are mathematically unavoidable.
Note that relying on what files "look like" in MS Excel is a number one mistake that you should avoid: MS Excel mangles data in all sorts of horrible ways that look indistinguishable from inside Excel, e.g. adding or changing dlimiters. It can also change data without any warning:
If you want reliable data processing do NOT open and save text files using MS Excel. It is a great tool for Excel spreadsheets... but for anything else... beware of dragons!
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Spreadsheets dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!