【问题标题】:reading formatted data from text file in matlab在matlab中从文本文件中读取格式化数据
【发布时间】:2015-11-24 22:53:59
【问题描述】:

我一直在尝试从格式化的文本文件中读取一些数字。 我想从众多列中保留一些列,并且我想将其重复到文件末尾(多行或多行)。

这是我为此编写的代码,但它只读取一行数据。

fid = fopen ('L0512164529','r+');
num_ints = 47;
num_rows = 50;
features = struct;

format =['L%d,',repmat('%f,' , 1 , num_ints-1),'%f'];
[r_dat,l] = textscan(fid, format, num_rows);
features.name=r_dat{1};
features.bodyposfeat=[r_dat{2:end}];

fclose(fid);

每一行都以一个以L 开头的数字开头。文件的前两行是:

L0512164529,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.1376599,-0.4387758,0.4723490,0.751‌​9389,0.4742642,-0.8703301
L0512164529,0.0001816,0.0000005,-0.0005697,-1.0843741,0.0001816,0.0000005,-0.000‌​5697,-1.0843741,0.1433973

【问题讨论】:

  • 举一个文件前几行的例子。
  • L0512164529,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.1376599,-0.4387758,0.4723490,0.7519389,0.4742642,-0.8703301..... L0512164529,0.0001816,0.0000005, -0.0005697,-1.0843741,0.0001816,0.0000005,-0.0005697,-1.0843741,0.1433973.....这些是文本的前两行。每行都以一个以 L 开头的数字开头。
  • 你的行有可变列?
  • 你也可以试试fid = fopen(file),然后使用while循环:while ~feofdata(k,:) = fread(fid);k=k+1;end
  • 如果行的列数不同,则 textscan 不是正确的工具。

标签: matlab text formatted


【解决方案1】:

确保您在格式字符串中使用了正确数量的%f 格式说明符。看来您可能需要使用以下格式声明:

format =['L%d,',repmat('%f,' , 1 , num_ints-2),'%f'];  %% changed to ( num_ints - 2)

假设num_ints 是列数。当我使用您的示例数据尝试您的代码时,当我将 num_ints 更改为 14 时它起作用了,因为您的示例只有 16 列,而第一个是用 L%d, 单独指定的。当我将 num_ints 增加到 14 以上时,它只解析文件的 1 行。

所以要清楚,如果 num_ints 是列数,包括具有 L 名称的第一列,那么在格式字符串创建中使用 num_ints-2 应该可以。

【讨论】:

  • 感谢您的回复,实际上我的文本文件中有很多行和列,我在评论中仅复制了其中的几个示例,让我澄清一下问题,我有 1385 行(行)和 907 列(没有 L-xxx 编号)我想使用 textscan 读取所有只有 47 列(或某些指定数量)列的行。
  • 好吧,就术语而言,如果您有可变数量的列(听起来像您这样做),那么在问题描述中说明这一点很重要。如果您想跳过没有 47 列的行,Textscan 将不适合您。我将用一个简单的替代方案添加另一个答案。
【解决方案2】:

如果您的文件中有可变数量的列,这是另一种方法。

filename = 'temp.txt'
fid = fopen(filename, 'rt');
if fid < 0
    error('Error opening file %s\n', filename); % exit point
end

desired_number_of_columns = 48;  %% 1 for the name, and 47 for the data
number_of_rows = 1385;  %% data structure won''t have this many rows, because some will be skipped

features.name=zeros(number_of_rows,1);
features.bodyposfeat=zeros(number_of_rows, desired_number_of_columns-1);

cntr = 1
while true
    tline = fgetl(fid);  %% read file one line at a time
    if ~ischar(tline), break; end; % break out of loop at end of file

    splitLine = strsplit(tline, ',');  %% separate into columns
    if (length(splitLine) ~= desired_number_of_columns)  %% check for the correct number of columns
        continue;
    end

    features.name(cntr) = str2num(splitLine{1}(2:end)); %% chop off first character and convert to number; does not check for 'L' specifically
    features.bodyposfeat(cntr, 1:desired_number_of_columns-1) = str2double(splitLine(2:end));  %% convert strings to numbers and save the rest of the columns as data
    cntr = cntr + 1;
end

%% optional - delete extra rows in the data structure that were never populated
features.name(cntr:end) = [];
features.bodyposfeat(cntr:end,:) = [];

fclose(fid)

【讨论】:

    猜你喜欢
    • 2017-08-06
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多