2012-10-01 261 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