【问题标题】:Read File Line by Line and Count in Matlab;save in one string在Matlab中逐行读取文件并计数;保存在一个字符串中
【发布时间】:2016-12-27 05:13:12
【问题描述】:

如何逐行读取此文件并计算长度?请帮忙。

numDim: 4
dim: 128 128 128 1
fov: 26.88 26.88 26.88  1.000
interval: 0.21 0.21 0.21 1.0
dataType: word
sdtOrient: ax
endian: ieee-le

【问题讨论】:

    标签: arrays string matlab readfile string-length


    【解决方案1】:

    如果你只是想:

    • 逐行读取文件
    • 将每一行存储在一个字符串中
    • 计算行数和每行的长度

    您可以使用以下功能:

    • fopen打开文件
    • fgetl 读取(在循环内)每一行
    • length 评估每一行的长度
    • fclose最后关闭输入文件

    您还应该使用计数器来计算行数。

    要存储该行,您可以使用cellarray

    这是一个可能的实现:

    % Open the input file
    fp=fopen('input_txt_file.txt','r');
    % Initialize a counter
    cnt=0;
    % Read the input file line by line
    while 1
       % Read the line
       tline = fgetl(fp);
       % Check for the end of file
       if ~ischar(tline)
          break
       end
       % Increment the counter once a line has been read
       cnt=cnt+1
       % Store the line in a cellarray
       str_cell{cnt}=tline
       % Get the length of the string
       str_len(cnt)=length(tline)
    end
    % Close the input file
    fclose(fp);
    

    如果您想读取输入文件并将“数字”值存储在数组中,除了上述函数之外,您还可以使用:

    • strtok` 扫描行

    然后您可以使用结构来保存输入;这允许您动态创建结构的字段,将它们分配为名称,每行的第一个字符串。

    switch 可用于识别特定行并相应地扫描数据。

    一个可能的实现可以是:

    % Open the input file
    fp=fopen('input_txt_file.txt','r');
    % Initialize a counter
    cnt=0;
    % Read the input file line by line
    while 1
       % Read the line
       tline = fgetl(fp);
       % Check for the end of file
       if ~ischar(tline)
          break
       end
       % Increment the counter once a line has been read
       cnt=cnt+1
       % Scan the line
       [token, remain]=strtok(tline,' ')
       % Store the data in a struct
       switch(token)
          case 'numDim:'
             data_struct.(token(1:end-1))=str2num(remain)
          case 'dim:'
             data_struct.(token(1:end-1))=str2num(remain)
          case 'fov:'
             data_struct.(token(1:end-1))=str2num(remain)
          case 'interval:'
             data_struct.(token(1:end-1))=str2num(remain)
          case 'dataType:'
             data_struct.(token(1:end-1))=remain(2:end)
          case 'sdtOrient:'
             data_struct.(token(1:end-1))=remain(2:end)
          case 'endian:'
             data_struct.(token(1:end-1))=remain(2:end)
       end
    
    end
    % Close the input file
    fclose(fp);
    

    这将创建以下输出结构:

    data_struct = 
    
           numDim: 4
              dim: [128 128 128 1]
              fov: [26.8800 26.8800 26.8800 1]
         interval: [0.2100 0.2100 0.2100 1]
         dataType: 'word'
        sdtOrient: 'ax'
           endian: 'ieee-le'
    

    希望这会有所帮助,

    卡普拉'

    【讨论】:

    • 而不是while 1,然后检查fgetl 是否返回char,最好只使用while ~feof(fp)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-31
    • 2021-07-13
    • 1970-01-01
    • 2019-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多