我想知道是否有可能读取如下所示的 .csv 文件:
0,0530,0560,0730,....
90,15090,15290,157....
我应该得到:
0,053 0,056 0,073 0,...
90,150 90,152 90,157 90,...
当使用 dlmread(path, '')
时,matlab 报错说
文件和格式字符向量不匹配。
无法读取“数字”字段 frin 文件(第 1 行,字段编号 2)==> ,053 0,056 0,073 ...
我也尝试过使用“0”作为分隔符,但 matlab 禁止这样做。
谢谢, 乔尼克斯
最佳答案
str= importdata('file.csv',''); %importing the data as a cell array of char
for k=1:length(str) %looping till the last line
str{k}=myfunc(str{k}); %applying the required operation
end
在哪里
function new=myfunc(str)
old = str(1:regexp(str, ',', 'once')); %finding the characters till the first comma
%old is the pattern of the current line
new=strrep(str,old,[' ',old]); %adding a space before that pattern
new=new(2:end); %removing the space at the start
end
和file.csv:
0,0530,0560,073
90,15090,15290,157
输出:
>> str
str=
'0,053 0,056 0,073'
'90,150 90,152 90,157'
关于matlab:分隔没有可用特定分隔符的 .csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45998639/