matlab:分隔没有可用特定分隔符的 .csv 文件

标签 matlab delimiter

我想知道是否有可能读取如下所示的 .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/

相关文章:

bash - 使用不同分隔符时 Bash cut 命令中的空格字符问题

bash - cut -d,忽略参数中的定界符

c++ - 我怎样才能使它与 C++ 中的每个定界符一起工作?

matlab - 如何在matlab中屏蔽图像的一部分?

c++ - MATLAB libpointer 数组

image - 从灰度图像和自定义颜色图创建 RGB 图像

Java分隔符读取文本文件

javascript拆分正则表达式

matlab - 为自己的类/对象重载加法 (+) 运算符

multithreading - 我的 matlab 代码的哪一部分是多线程的?