【问题标题】:Matlab: read and calculate percentage of occurrence values from text filesMatlab:从文本文件中读取并计算出现值的百分比
【发布时间】:2013-04-25 11:36:17
【问题描述】:

我有一组名称为 .txt 的文件: table.iterations.txt 其中iterations = 1:10000 (所以它是table.01.txttable.02.txttable.1001.txt 等,每个文件大小小于 2kb)。 每个 txt 文件在不同的行中包含值、不带小数的整数 p.e.:

table.01.txt  table.02.txt ... table.1001.txt
 2              5               32
 5             19               37
19             45               58
52             88               62 
62             89               75
95                              80
99                              88
                               100   

每个txt文件可以包含不同数量的值,其中0<value<101

我需要有关如何读取所有这些文件以查找其值在所有 txt 文件中出现的百分比的帮助。 在上面的粗略示例中,值 2 出现一次,值 5 出现两次,值 100 出现一次,等等。

提前谢谢你。

【问题讨论】:

  • 尝试为一个文件执行此操作并发布您的代码?然后查看dir(*.txt) mathworks.com/help/matlab/ref/dir.html 以读取所有文件。我建议你创建一个名为occurrences 的数组,它有 102 个元素长,然后每次遇到数字 n 时,你只需 occurences(n+1) = occurences(n+1) + 1
  • 一旦所有数字都加载到变量中,[histc(X, unique(X)), unique(X)] 将为您提供出现次数的直方图。从那里转换为百分比应该很容易
  • @user1207217 你不是说histc(X, 0:101)吗?
  • @Dan:我已经完成了,正在为我的解决方案添加最新更新。

标签: matlab file-io find-occurrences


【解决方案1】:

来自cmets,据this post

dirName = 'C:\yourpath';                           %# folder path
files = dir( fullfile(dirName,'table.*.txt') );    %# list all *.txt files, make sure you have only the txt's you are interested on inside selected path
files = {files.name}';                             %# file names
data = cell(numel(files),1);                       %# store file contents
for i=1:numel(files)  
    fname = fullfile(dirName,files{i});            %# full path to file
    values{i}=load(fname);                         %# load values from txt to variable
    data{i} = histc(values{i},1:100);              %# find occurences, for max value =25 change 100 to 25
end

thestructdata=[data{:}];                           %# convert to matrix
for j2=1:size(thestructdata,1)
    occ(j2,:)=histc(thestructdata(j2,:),1);        %# find the number of occurence, 1 is present, on each line on all txt files 
end
occ=[occ]';                                        %# gather results to an array
occperce=occ(1,:)./numel(files)*100                %# results in percentage, max value = 25, change to 100 if needed as the OP question

结果(25 个值的最大值):

occ =    
    14    11    10    12    13    15    11    10    11    10     7    14    11    12    11    13     7    11    10    12    14    12    13    14    11


occperce =

  Columns 1 through 20

   56.0000   44.0000   40.0000   48.0000   52.0000   60.0000   44.0000   40.0000   44.0000   40.0000   28.0000   56.0000   44.0000   48.0000   44.0000   52.0000   28.0000   44.0000   40.0000   48.0000

  Columns 21 through 25

   56.0000   48.0000   52.0000   56.0000   44.0000

如果你愿意,你可以这样做删除所有 txt 文件:delete(dirName,'table.*.txt');

【讨论】:

    猜你喜欢
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 2015-10-11
    相关资源
    最近更新 更多