%% txt文本数据中空格改成逗号代码实现
clear
clc
close all
% 原始数据所在路径
orgpath = 'E:\database\\';
% 查看数据列表
filelistName = 'filelist.txt';
filelist = strcat(orgpath,filelistName);
fidfile = fopen(filelist,'r');
if fidfile < 0
error('数据文件名打开错误');
end
% 结果所在路径
respath = 'E:\database1\';
tline = fgetl(fidfile);
fileno = 0;
while ischar(tline)
rname = strtrim(tline);
if (1 == strncmp(rname,'#',1))
tline = fgetl(fidfile);
continue;
end
fileno = fileno + 1;
loadtxt = strcat(orgpath,rname,'.txt');
lt = load(loadtxt);
createtxt = strcat(respath,rname,'.txt');
fid = fopen(createtxt,'w');
for i = 1:length(lt)
fprintf(fid,'%d,%d,%d,%d,%d\n',lt(i,1),lt(i,2),lt(i,3),lt(i,4),lt(i,5));
end
fclose(fid);
%% 读入下一条数据
tline = fgetl(fidfile);
close all
end
fclose(fidfile);
本文介绍了一个使用Matlab编写的程序,该程序能够批量处理指定目录下的文本文件,将文件中的空格替换为逗号,并将处理后的文件保存到另一个指定目录。此方法适用于需要统一文本数据格式的场景。

942

被折叠的 条评论
为什么被折叠?



