遇到一个初级问题,请教各位,先谢谢了!
我的工作路径 C:\input
下面有若干个文件夹,文件夹名字不规则,每个文件夹内又有若干个*.dat数据文件,文件名也是不规则排列的。
我想依次读取每个文件夹内的每个文件(按字母排列顺序来),然后做相关运算。
我的问题写在命令的注释里面了,请帮我看看,或者有别的更高效简单的方法,请不吝赐教:
clc; clear;
n = 1;
dir_a = dir('C:\input\');
fn_a = struct2cell(dir_a); %获得所有文件夹名的cell
for i = 3 : size(fn_a,2); %按上面的命令,发现第一个文件夹的名字是从fn_a(1,3)才开始,
%前面两个fn_a(1,1)和fn_a(1,2)分别是'.'和'..',这个我还不明白?
dir_b = dir(strcat('C:\input\',fn_a(1,i),'\*.dat');
%发现这个命令出错!程序中止,是不是不能这么用?请教各位。
fn_b = struct2cell(dir_b); %获得每个文件夹内所有数据文件的cell
for j = 1 : size(fn_b,2);
file_input = strcat('C:\input\',fn_a(1,i),'\',fn_b(1,j));
file_output = strcat('C:\output\',int2str(n),'.dat');
n = n + 1;
fid1 = fopen(file_input, 'r');
fid2 = fopen(file_output, 'w');
...
...
fclose(fid2);
fclose(fid1);
end
end
回答:
1、有点是因为dir默认的全部;
2、修改成
dir_b = dir( cell2mat(strcat('C:\input\',fn_a(1,i),'\*.dat')) )