有50个txt文件,只读取每个txt文件的第10行,然后生成50行的excle表格,txt文件大概就是下面这张图,这个好像还用到sort_nat自然排序的函数,希望能得到高人的编程指点!多谢
类似这种任务可以拆分成这么几个步骤
(1)循环获取文件夹内的txt文件名称——使用dir函数
(2)读txt文件时,把第50行提出来,存进矩阵——使用textscan函数
(3)把矩阵转成excel——使用xlswrite函数
对于提取txt可以看一下我的文章
https://blog.csdn.net/weixin_44276743/article/details/127026036?spm=1001.2014.3001.5502
https://blog.csdn.net/weixin_44276743/article/details/127269690?spm=1001.2014.3001.5502
里面提到的一些内容
%首先你要把所有txt放在一个文件夹里,这个文件夹里只有这些txt,就比较好办
%代码中你需要替换或修改的变量有:path,Format,filename
info=dir((fullfile(path, '\*.txt'))); %获取需要更改的文件们的信息,只选txt格式的,path替换成你自己放txt的文件夹地址
len=length(info); %计数一共几个文件
for i=1:len %准备循环访问这些文件
name=info(i).name; %获取第i个文件的名字 (*.txt这样的带后缀的全名)
fpathname=[path '\' name]; %获取带地址的全名
fn= fopen(fpathname);%打开这个文件
Format=repmat('%f %f %f %f %f %f' ,1,1); %这里你一行有多少个数,就写多少个%f
data = textscan(fn,Format,1,'headerlines',49);
%参数1:从fn里
%参数2:以Format的格式提取值(比如现在写的代码就是6个float格式,即六个%f
%参数3:提取1行
%参数4:跳过前n行
%参数5:跳过的n为49,即提取第50行
fclose(fn); %记得有开有关,不然下次会报错
mdata=cell2mat(data); %用textscan提取出的内容应该是一个cell格式的,转成行向量格式,这样方便储存
Alldata(i,:)=madata; %创建一个变量Alldata储存每个文件第50行的内容
end
%循环结束,你想要的数据应该已经在Alldata里了
%写入xls
xlswrite(filename,Alldata); %filename是你自己创建的想存这个数据的带路径的文件名,大概是’XXX\YY.xls'这种格式。
不知道你这个问题是否已经解决, 如果还没有解决的话:由于在上述代码中,如果你的批量图片名存在为:1.png;2.png;…1001.png;…1010.png.你会发现在用dir读取的files.name的特性中,排序放式并不是你想要的1,2,,,这种方式,而是1,1001,1010,…,2,…这种方式。
所以为了能够按十进制的排序方式,采用sort_nat()函数,对files.name 进行排序。其中调用sort_nat()方式和sort_nat()函数内容如下。
%调用sort_net()
clear;
clc;
files = dir(strcat('F:\2019春\课题总结实验1\未改进代码\InverseHalftoning-master\train_color\cmyk\train\raw\','.PNG'));
files_name =sort_nat({files.name})
len=length(files);
for i=1:len
oldname=files_name{i};
newname=strcat(num2str(i),'.png')
eval(['!rename' 32 oldname 32 newname]);
end
这里值得注意的时是函数的调用方式是files_name =sort_nat({files.name}),而不是files_name =sort_nat(files.name),否则会出报错为错误使用 sort_nat,输入参数太多。”
%sort_nat具体内容
function [cs,index] = sort_nat(c,mode)
%sort_nat: Natural order sort of cell array of strings.
% usage: [S,INDEX] = sort_nat(C)
%
% where,
% C is a cell array (vector) of strings to be sorted.
% S is C, sorted in natural order.
% INDEX is the sort order such that S = C(INDEX);
%
% Natural order sorting sorts strings containing digits in a way such that
% the numerical value of the digits is taken into account. It is
% especially useful for sorting file names containing index numbers with
% different numbers of digits. Often, people will use leading zeros to get
% the right sort order, but with this function you don't have to do that.
% For example, if C = {'file1.txt','file2.txt','file10.txt'}, a normal sort
% will give you
%
% {'file1.txt' 'file10.txt' 'file2.txt'}
%
% whereas, sort_nat will give you
%
% {'file1.txt' 'file2.txt' 'file10.txt'}
%
% See also: sort
% Version: 1.4, 22 January 2011
% Author: Douglas M. Schwarz
% Email: dmschwarz=ieee*org, dmschwarz=urgrad*rochester*edu
% Real_email = regexprep(Email,{'=','*'},{'@','.'})
% Set default value for mode if necessary.
if nargin < 2
mode = 'ascend';
end
% Make sure mode is either 'ascend' or 'descend'.
modes = strcmpi(mode,{'ascend','descend'});
is_descend = modes(2);
if ~any(modes)
error('sort_nat:sortDirection',...
'sorting direction must be ''ascend'' or ''descend''.')
end
% Replace runs of digits with '0'.
c2 = regexprep(c,'\d+','0');
% Compute char version of c2 and locations of zeros.
s1 = char(c2);
z = s1 == '0';
% Extract the runs of digits and their start and end indices.
[digruns,first,last] = regexp(c,'\d+','match','start','end');
% Create matrix of numerical values of runs of digits and a matrix of the
% number of digits in each run.
num_str = length(c);
max_len = size(s1,2);
num_val = NaN(num_str,max_len);
num_dig = NaN(num_str,max_len);
for i = 1:num_str
num_val(i,z(i,:)) = sscanf(sprintf('%s ',digruns{i}{:}),'%f');
num_dig(i,z(i,:)) = last{i} - first{i} + 1;
end
% Find columns that have at least one non-NaN. Make sure activecols is a
% 1-by-n vector even if n = 0.
activecols = reshape(find(~all(isnan(num_val))),1,[]);
n = length(activecols);
% Compute which columns in the composite matrix get the numbers.
numcols = activecols + (1:2:2*n);
% Compute which columns in the composite matrix get the number of digits.
ndigcols = numcols + 1;
% Compute which columns in the composite matrix get chars.
charcols = true(1,max_len + 2*n);
charcols(numcols) = false;
charcols(ndigcols) = false;
% Create and fill composite matrix, comp.
comp = zeros(num_str,max_len + 2*n);
comp(:,charcols) = double(s1);
comp(:,numcols) = num_val(:,activecols);
comp(:,ndigcols) = num_dig(:,activecols);
% Sort rows of composite matrix and use index to sort c in ascending or
% descending order, depending on mode.
[unused,index] = sortrows(comp);
if is_descend
index = index(end:-1:1);
end
index = reshape(index,size(c));
cs = c(index);