Remove equal sign and quotation marks in readtable
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Tintin Milou
el 7 de Abr. de 2022
Comentada: Stephen23
el 7 de Abr. de 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 comentarios
Stephen23
el 7 de Abr. de 2022
Editada: Stephen23
el 7 de Abr. de 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.
Respuesta aceptada
Stephen23
el 7 de Abr. de 2022
Editada: Stephen23
el 7 de Abr. de 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 comentarios
Stephen23
el 7 de Abr. de 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{:})
Más respuestas (0)
Ver también
Categorías
Más información sobre Spreadsheets en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!