【问题标题】:how to get number of digits from a text file in matlab如何从matlab中的文本文件中获取位数
【发布时间】:2016-10-24 23:09:26
【问题描述】:

我正在尝试读取文本文件,然后计算文件中包含的位数(数字 0 到 9)。我使用 fid = fopen('filename','r') 打开文件,然后我使用 textscan(fid,'%f') 尝试获取数字,但它返回一个空(0 x 1)矩阵。我也使用了 fscanf 但不起作用。我认为这是错误的格式规范,但使用其他格式规范不起作用。请指教

【问题讨论】:

    标签: matlab


    【解决方案1】:

    首先,格式规范%f 尝试将文件中的所有数字读取为浮点数,这不是您想要的,看起来不是。

    如果您想要的只是文件中数字的数量,只需将整个内容作为字符串加载并搜索数字 0-9。

    fid = fopen('filename', 'r');
    characters = fread(fid, '*char');
    fclose(fid);
    
    % Determine whether each character in the input was a digit between 0 and 9
    isDigit = ismember(characters, '0':'9');
    
    % Count the total number of characters that were digits
    nDigits = sum(isDigit);
    

    【讨论】:

      猜你喜欢
      • 2015-05-02
      • 2011-04-04
      • 2013-10-16
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      • 2010-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多