2012-10-01 177 views
0

我试图编写一个代码来检查文件是否打开。如果是这样,当文件未关闭时,将在屏幕上显示警告消息。matlab:检查xls文件是否打开

我想我并没有因为这样正确地使用“FCLOSE”,我得到了错误:

??? Error using ==> fclose 
Invalid file identifier. Use fopen to generate a valid file identifier. 

Error in ==> fileisopen at 4 
fclose(fid); 

我想没有FCLOSE功能,它的工作原理,但是当我打开文件我有一个消息,该文件只用于阅读。

这是我的代码:

path_of_file = 'C:\Documents and Settings\erezalon\Desktop\data.xls'; 

[fid msg_file] = fopen(path_of_file, 'a'); 
fclose(fid); 
while (fid == -1) 
    errormsg = strcat('the file: ', path_of_file, ' is open. please close it!'); 
    waitfor(msgbox(errormsg,'Error')); 
    [fid msg_file] = fopen(path_of_file, 'a'); 
    fclose(fid); 
end 
+0

您是否有相关文件(data.xls)的写入权限? – aganders3

+0

@ aganders3,你是什么意思?谢谢。 –

回答

0

我succeded!这是解决方案:

path_of_file = 'C:\Documents and Settings\erezalon\Desktop\data2.xls'; 

fid = fopen(path_of_file, 'a'); 
if fid ~= -1 
    fclose(fid); 
else 
    while (fid == -1) 
     errormsg = strcat('the file: ', path_of_file, ' is open. please close it!'); 
     waitfor(msgbox(errormsg,'Error')); 
     fid = fopen(path_of_file, 'a'); 
    end 
    fclose(fid); 
end