Remove equal sign and quotation marks in readtable
17 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Tintin Milou
am 7 Apr. 2022
Kommentiert: Stephen23
am 7 Apr. 2022
I am trying to read a csv file that includes both text and numbers. Here is a screenshot of the file.
I am using the following command:
tmp = readtable('file.csv','Format','auto','ReadVariableNames',0);
The tmp file is full of equal signs and quotation marks, which are indeed in the csv file, but are not displayed in excel. For some reason, when I run the same code on a different machine, the tmp file looks much nicer because it does not have the quotation marks and equal signs. I don't know if that is due to the way matlab or excel are setup. In both cases, it's Matlab 2020b.
Is there a possibility to always remove the equal signs and quotation marks when reading the table? I am looking for a robust solution that ensures that the code can be run on different machines.
Thanks!
4 Kommentare
Stephen23
am 7 Apr. 2022
Bearbeitet: Stephen23
am 7 Apr. 2022
"Here is a screenshot of the file. "
No, that is just how some random Application displays it. Excel is an unreliable tool for viewing file content, it can even change data without warning:
The actual text file looks like this:
It appears that every single field has been defined as an Excel formula, rather than as the actual text or numeric data. The application that created that file is trying to make everyone's lives as difficult as possible.
Akzeptierte Antwort
Stephen23
am 7 Apr. 2022
Bearbeitet: Stephen23
am 7 Apr. 2022
This should get you started. Note that you can tailor the range, etc.
fnm = 'iv1_gdp_cum.csv';
opt = {'Delimiter',{',','='}, 'ReadVariableNames',false,...
'ConsecutiveDelimitersRule','join', 'LeadingDelimitersRule','ignore'};
tbl = readtable(fnm,opt{:})
2 Kommentare
Stephen23
am 7 Apr. 2022
"Is there a way to read in the numbers in parentheses rather than replacing them by NaN?"
Ah, I forgot about those. One approach might be to define the parentheses as being whitespace:
fnm = 'iv1_gdp_cum.csv';
opt = {'Delimiter',{',','='}, 'ReadVariableNames',false, 'Whitespace',' \t\b()',...
'ConsecutiveDelimitersRule','join', 'LeadingDelimitersRule','ignore'};
tbl = readtable(fnm,opt{:})
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Spreadsheets finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!