PDA

查看完整版本 : [MATLAB基础] matlab的exist命令在循环语句中的问题


guohf
2013-08-21, 11:48
我要对一个文件夹中的全部文件进行处理,用了循环语句,但在用exist判断某文件是否存在(若存在进行其他处理)出错。简单程序如下:

clear; clc;
for i = 1:1000;
file2=['G:\datapressure\xicy2-0' num2str(i)]; %文件xicy2-01,xicy2-02....含有字符
if exist('file2','file')==2 % 因个别文件不存在,故用这个语句来判断,但整个循环中exist('file2','file')的值全为0,下面语句不执行
data_all2=importdata(file2);
data2=data_all2.data;
.......
end
end

若用exist('G:\datapressure\xicy2-01','file') 当文件存在时值是2,不存在值为0。但有上千文件,不能手动处理。请教高手上面程序要怎样修改啊??或者,不用exist也行,只要能判断某文件是否存在,以便对存在文件进行处理就行。先谢了

kaituozhe528
2013-08-30, 00:32
I think you should add the file extension. Your file path is not complete so the file can not be found, as a result, the returned value is 0 not 2 expected.