我有一组名称为.txt的文件:
table.iterations.txt
其中iterations = 1:10000
(table.01.txt
,table.02.txt
,table.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出现一次等。
提前谢谢。
答案 0 :(得分:1)
来自评论,根据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');