How can I remove characters in cvs file?
7 次查看(过去 30 天)
显示 更早的评论
Hi!
I have several csv files with the information strutured as
"1";"2";"3";
and I need to access the informations as matrixes.
I have tried readmatrix, and readmtx, but the " " are really bothering me. When I tried readtable, it gets the information but doesn't save it as a number, so potentially I could transform the table to a matrix, but I haven't been able to.
I tried to use:
T2= replace(T1,' " ',' ')
Any help would be appreciated! Thanks!!
采纳的回答
Stephen23
2021-3-18
编辑:Stephen23
2021-3-18
Using string manipulation:
str = fileread('prueba1.csv');
str = strrep(strrep(str,'"',''),';',' ');
mat = sscanf(str,'%f',[3,Inf]).'
Or using TEXTSCAN:
fmt = '%f%f%f';
opt = {'Delimiter','";', 'MultipleDelimsAsOne',true, 'CollectOutput',true};
fid = fopen('prueba1.csv','rt');
tmp = textscan(fid,fmt,opt{:});
fclose(fid);
mat = tmp{1}
更多回答(1 个)
Seth Furman
2021-3-18
编辑:Seth Furman
2021-3-18
If you use readtable, you can convert the table to a matrix by using the table2array function.
For example,
>> readtable('prueba1.csv')
ans =
5×3 table
Var1 Var2 Var3
____ ____ ____
234 42 243
456 34 23
678 75 765
456 355 65
647 78 75
>> table2array(ans)
ans =
234 42 243
456 34 23
678 75 765
456 355 65
647 78 75
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!