【问题标题】:textscan with different format data具有不同格式数据的文本扫描
【发布时间】:2017-07-03 11:16:17
【问题描述】:

我有一个数据有两种格式,我试图在 MATLAB 中使用 textscan 进行扫描

1,2,3,four,five,6
7,8,9,ten,eleven,12

以此类推,然后是另一组数据,格式如下

1,2,three,4
5,6,seven,8

我正在尝试使用

扫描此数据
fid=fopen('data.txt');    
C=textscan(fid,'%f %f %f %s %s %f','delimiter',',');
D=textscan(fid,'%f %f %s %f','delimiter',',');

整个数据看起来像

1,2,3,four,five,6
7,8,9,ten,eleven,12

1,2,three,4
5,6,seven,8

不同格式的数据用空行分隔,问题是C也从第二个表中获取数据,最多1,2因此打乱了D的格式

遇到空行如何停止textscan

【问题讨论】:

  • 默认情况下,textscan 在遇到格式错误时停止并返回它在行之前所做的一切。您想要的是阅读所有内容,然后按 2 种格式过滤

标签: matlab textscan


【解决方案1】:

您可能想查看 matlab 的 readtable 函数。 https://www.mathworks.com/help/matlab/ref/readtable.html

我已使用此功能读取由逗号分隔的包含文本和数字的文件,其中 textscan 很难。然后可以将此表转换为元胞数组或矩阵,以进行您想要对其进行的任何处理。

【讨论】:

  • readtable() 只返回我要存储所有表的最后一个表!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-13
  • 2021-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多