Reading a CSV File that has not Quoted its Strings

2 vues (au cours des 30 derniers jours)
Case Martin
Case Martin le 24 Mar 2020
Modifié(e) : Star Strider le 25 Mar 2020
I am currently writing a script to do Data Analysis that as previously done with a lot a Copy and Paste in Excel. I have a .CSV file that contains all of my data. I have successfully imported it, but now I am having trouble reading it. All of the values are comma deliminataed. However, the string values are not in single quotes; they are not quoted at all.
The error is "Error using dlmread (line 147)," and I believe it is because MATLAB expects quotes around the strings or it is expecting a numberic value because there are no quotes.
filename = uigetfile('*.*','Select image file','ON'); %Sets file name
addpath(genpath('foldername')); %Folder name will change, set older to folder shared between script and file
unformatteddata = csvread(filename);
I have attatched the .CSV file for reference.
How do I read a CSV file that has not properly quoted its strings?

Réponse acceptée

Star Strider
Star Strider le 24 Mar 2020
I cannot get csvread to imoprt that file at all.
However readtable has no problems with it:
T1 = readtable('T1811061.24721.csv', 'PreserveVariableNames',1);
FirstFiveRows = T1(1:5,:)
producing:
FirstFiveRows =
5×6 table
Time (sec) Load (N) Stress (MPa) Strain (mm) Position (mm) Cycle
__________ ________ ____________ ___________ _____________ _____
0 5.04 {'N/A'} 0.00086 0.006 1
0.1 20.93 {'N/A'} 0.00106 0.008 1
0.2 40.66 {'N/A'} 0.00131 0.011 1
0.3 64.73 {'N/A'} 0.00162 0.015 1
0.4 88.3 {'N/A'} 0.00193 0.018 1
  3 commentaires
Jeremy Hughes
Jeremy Hughes le 24 Mar 2020
Try this:
>> opts = detectImportOptions(filename)
>> t = readtable(filename,opts)
In R2020a this is the same as,
>> t = readtable(filename)
Star Strider
Star Strider le 24 Mar 2020
Modifié(e) : Star Strider le 25 Mar 2020
In R2020a I was able to read it without problems, however I reproduced the error running it in R2019b with all the latest Updates.
The offending line is:
Line2631 =
1×6 table
Time (sec) Load (N) Stress (MPa) Strain (mm) Position (mm) Cycle
__________ ________ ____________ ___________ _____________ _____
NaN NaN {0×0 char} NaN NaN NaN
There may not be a workaround for that.
When I try specifying the row range in R2020a it works, however R2019b does not recognise the Range parameter, even though it’s in the R2019b documentation:
T1 = readtable('Case Martin T1811061.24721.csv', 'PreserveVariableNames',1, 'Range','1:2630');
Try this anyway and see if it works. That’s the only option I can offer.
The only other option may be to upgrade to R2020a.
EDIT — (25 Mar 2020 at 17:30)
The problem appears to be an essential incompatibility with the .csv format and the R2019b version of readtable. The only reliable way to get around that may be to get into Excel itself and save a version of the file as a text file. I saved it as a MS-DOS text file (I’m running Win 10), and then was able to import it successfully with:
T1 = readtable('Case Martin T1811061.24721.txt', 'PreserveVariableNames',1)
(I attach names to files so I know what they refer to.) I have attached that file here.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by