【问题标题】:Trouble loading in mixed data from txt file MATLAB从 txt 文件 MATLAB 中加载混合数据时遇到问题
【发布时间】:2018-05-30 16:03:24
【问题描述】:

我正在尝试从文本文件中加载数据。前两行是标题,在标题之后的前两列是日期和时间。其余列是浮点数。

数据应该有 11 列,但是,whos 返回大小仅为 1x3

数据txt文件:

fid = fopen('allunderway.txt', 'rt');
data = textscan(fid, '%{M/dd/yyyy}D %{HH:mm:ss}D %4.2f %2.4f %2.5f %2.4f %2.4f %2.2f %4.2f %3.1f %1.4f', 'HeaderLines', 2, 'CollectOutput', true);
fclose(fid);
whos data
date = data{1};
time = data{2};
wnd_td = data{10};
wnd_ts = data{11};

【问题讨论】:

  • 您尝试了read_table 函数吗?否则,您能否将您的数据文件提供为 txt 以便尝试?
  • 我尝试了readtable,得到了Warning: Unable to determine the format of the DATETIME data. ...... Warning: Variable names were modified to make them valid MATLAB identifiers.
  • 我尝试添加 %D ... T = readtable('allunderway.txt', '%{M/dd/yyyy}D %{HH:mm:ss}D %4.2f %2.4f %2.5f %2.4f %2.4f %2.2f %4.2f %3.1f %1.4f', 'HeaderLines', 2);readtable 的错误表明争论的数量错误

标签: matlab file matrix load data-files


【解决方案1】:

您可以尝试使用分隔符,看起来这是一个制表符分隔的文件。 您可能必须在 fopen 命令中同时尝试“rt”和“r”。 至于 textscan 部分,请尝试添加此

'Delimiter','\t','EmptyValue',NaN

它添加制表符作为分隔符并用 NaN 替换空值。
或使用空格作为分隔符并设置它,以便有 1 个或多个空格无关紧要

'Delimiter',' ','MultipleDelimsAsOne',1

或使用“空白”作为分隔符(同时使用制表符和空格)。

【讨论】:

  • @Rose 如果这对你有帮助,请点赞。如果解决了你的问题,请采纳。如果您遇到问题并需要帮助,请回复并询问。
猜你喜欢
  • 2020-05-07
  • 1970-01-01
  • 1970-01-01
  • 2017-12-19
  • 1970-01-01
  • 1970-01-01
  • 2021-08-25
  • 2018-09-05
  • 2020-07-13
相关资源
最近更新 更多