使用Matlab读取txt数据(将多个txt文件中的数据合并)
file = dir('result\*.txt'); %读取文件夹目录
out = [];
for i = 1 : length(file)
in = load(['result\',file(i).name]);%依次载入文件(仅限于数字)
out = [out, in']; %合并txt中的内容,存入矩阵中
end
fid = fopen('out.txt','wt'); %将文件读出
fprintf(fid,['%g ', '%g','\n'],out); %格式输出
%fprintf(fid,[repmat('%g ',1,size(out,2)), '\n'],out);
fclose(fid);
另一种读入的方式:
all_name = fopen('All.txt');
line = fgetl(all_name);%逐行读入(包括数字,字符串)
count = 1;
while ischar(line)
str{count,:} = line; %按行存入cell中
line = fgetl(all_name);
count = count + 1;
end
fclose(all_name);