Remove equal sign and quotation marks in readtable

17 Ansichten (letzte 30 Tage)
Tintin Milou
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
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.
Tintin Milou
Tintin Milou am 7 Apr. 2022
I am schizophrenic sometimes... I created the file using the esttab command in stata. Seems to be the standard way of writing regression results from stata to csv.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
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{:})
tbl = 39×10 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 _________________ ______ ______ ______ _____ ____ ____ ____ ____ _____ {'gov_cum0' } 0.552 NaN NaN NaN NaN NaN NaN NaN NaN {0×0 char } NaN NaN NaN NaN NaN NaN NaN NaN NaN {'tax_aggr_cum0'} -1.066 NaN NaN NaN NaN NaN NaN NaN NaN {0×0 char } NaN NaN NaN NaN NaN NaN NaN NaN NaN {'gov_cum1' } NaN 0.82 NaN NaN NaN NaN NaN NaN NaN {0×0 char } NaN NaN NaN NaN NaN NaN NaN NaN NaN {'tax_aggr_cum1'} NaN -1.275 NaN NaN NaN NaN NaN NaN NaN {0×0 char } NaN NaN NaN NaN NaN NaN NaN NaN NaN {'gov_cum2' } NaN NaN 0.928 NaN NaN NaN NaN NaN NaN {0×0 char } NaN NaN NaN NaN NaN NaN NaN NaN NaN {'tax_aggr_cum2'} NaN NaN -1.479 NaN NaN NaN NaN NaN NaN {0×0 char } NaN NaN NaN NaN NaN NaN NaN NaN NaN {'gov_cum3' } NaN NaN NaN 0.918 NaN NaN NaN NaN NaN {0×0 char } NaN NaN NaN NaN NaN NaN NaN NaN NaN {'tax_aggr_cum3'} NaN NaN NaN -1.7 NaN NaN NaN NaN NaN {0×0 char } NaN NaN NaN NaN NaN NaN NaN NaN NaN
  2 Kommentare
Tintin Milou
Tintin Milou am 7 Apr. 2022
That's great. Thanks. Is there a way to read in the numbers in parentheses rather than replacing them by NaN?
Stephen23
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{:})
tbl = 41×10 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 _________________ ______ ______ ______ _____ ____ ____ ____ ____ _____ {0×0 char } 1 2 3 4 5 6 7 8 9 {0×0 char } NaN NaN NaN NaN NaN NaN NaN NaN NaN {'gov_cum0' } 0.552 NaN NaN NaN NaN NaN NaN NaN NaN {0×0 char } 0.243 NaN NaN NaN NaN NaN NaN NaN NaN {'tax_aggr_cum0'} -1.066 NaN NaN NaN NaN NaN NaN NaN NaN {0×0 char } 0.326 NaN NaN NaN NaN NaN NaN NaN NaN {'gov_cum1' } NaN 0.82 NaN NaN NaN NaN NaN NaN NaN {0×0 char } NaN 0.279 NaN NaN NaN NaN NaN NaN NaN {'tax_aggr_cum1'} NaN -1.275 NaN NaN NaN NaN NaN NaN NaN {0×0 char } NaN 0.393 NaN NaN NaN NaN NaN NaN NaN {'gov_cum2' } NaN NaN 0.928 NaN NaN NaN NaN NaN NaN {0×0 char } NaN NaN 0.283 NaN NaN NaN NaN NaN NaN {'tax_aggr_cum2'} NaN NaN -1.479 NaN NaN NaN NaN NaN NaN {0×0 char } NaN NaN 0.398 NaN NaN NaN NaN NaN NaN {'gov_cum3' } NaN NaN NaN 0.918 NaN NaN NaN NaN NaN {0×0 char } NaN NaN NaN 0.293 NaN NaN NaN NaN NaN

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by