【问题标题】:how to remove the header line of .txt file while not disturbing the rest of the line in matlab如何在不干扰matlab中其余行的情况下删除.txt文件的标题行
【发布时间】:2013-10-15 23:13:50
【问题描述】:

尊重所有人,

我在将 .txt 文件读入我的 matlab 程序时遇到问题。我的仪器给出的数据为

绝对参考气溶胶采样器 $ 1 2 149; 2 3 183 美元; 3 3 191 美元; 4 3 210 美元;

我必须阅读它并将其写入 excel。我正在使用以下代码从文件中读取数据,但它给出了错误。

[filename,pathname] = uigetfile('*.txt','SELECT THE RAW DATA FILE');
filepath = fullfile(pathname,filename);
fid = fopen(filepath);
c = textscan(fid,'$%n%n%n;','headerlines','1');

请任何人建议我如何删除 .txt 文件中的标题部分,而不会影响行的其余部分。

【问题讨论】:

  • 文件的其余部分是如何格式化的,是 CSV 吗?
  • 1.) 你得到什么错误? 2.)您的文件中是否有换行符,还是只有一行(如您在问题中所示)?

标签: matlab


【解决方案1】:

您可以使用textscan 并忽略它返回的元胞数组的第一列:

一些.txt:

myheader 156
myheader 158

代码:

fileID = fopen('some.txt');
C = textscan(fileID,'%s %f', 'delimiter',' ');
fclose(fileID);
celldisp(C)

返回:

C{1}{1} =

myheader


C{1}{2} =

myheader


C{2} =

   156
   158

【讨论】:

  • textscan 有一个输入选项 Headerlines 专门处理这个问题。见help textscan
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-14
  • 2021-01-25
  • 2013-07-12
  • 1970-01-01
相关资源
最近更新 更多