2014-10-31 56 views
0

我一直在試圖打開循環中的文件。我這樣做:試圖在循環matlab中打開文件

file=''; 
loc='F:\UT_timestep\'; 
name='time_'; 
gridext='.grd'; 
for i={'a','b','c'} 
    file=strcat(loc,name,i,gridext); 
    f=fopen(file,'rb'); 
    ... 
    fclose(f); 
end 

,但它給出了這樣的錯誤:使用的fopen
首先輸入必須是char類型的文件名,或者double類型的文件標識符

錯誤。

script_UT(第28行)中的錯誤 f = fopen(file,'rb');

我不明白爲什麼這是給錯誤。請幫忙。

回答

2

這是因爲file是1元素的單元陣列。你需要單元格數組中的實際字符串,而不是實際的單元格本身。這樣做:

file=''; 
loc='F:\UT_timestep\'; 
name='time_'; 
gridext='.grd'; 
for i={'a','b','c'} 
    file=strcat(loc,name,i,gridext); 
    f=fopen(file{1},'rb'); %// Change 
    ... 
    fclose(f); 
end 
+0

thanks..worked fine now – Vid 2014-10-31 15:28:25