0

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

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 出现一次,依此类推。

先感谢您。

4

1 回答 1

1

从评论中,根据这篇文章

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-04-25T15:56:05.763 回答