Remove equal sign and quotation marks in readtable
15 次查看(过去 30 天)
显示 更早的评论
I am trying to read a csv file that includes both text and numbers. Here is a screenshot of the file.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/955615/image.png)
I am using the following command:
tmp = readtable('file.csv','Format','auto','ReadVariableNames',0);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/955620/image.png)
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 个评论
Stephen23
2022-4-7
编辑:Stephen23
2022-4-7
"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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/955705/image.png)
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.
采纳的回答
Stephen23
2022-4-7
编辑:Stephen23
2022-4-7
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 个评论
Stephen23
2022-4-7
"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{:})
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!