作者xj04a83 (shin)
看板MATLAB
標題[問題] 使用textread+dir連續讀檔
時間Thu Jul 19 15:20:21 2012
如題
在資料夾裡面的檔案名稱不連續
ex. 21.txt 23.txt 31.txt
現在想用textread將所有檔案讀進來並做處理
初步程式碼
path = 'C:\';
file = dir(path);
filename = {file(~[file.isdir]).name};
for i = 1:3
name = filename(i);
fullname = strcat(path,name);
[name number] = textread(fullname,'%f &f',-1,'headerlines',1);
end
但卻跑出
??? Undefined function or method 'exist' for input arguments of type 'cell'.
Error in ==> textread>noargname at 192
arg = f(arg);
Error in ==> textread at 159
if (noargname(@exist,varargin{1}) ~= 2 ||
noargname(@exist,fullfile(cd,varargin{1})) ~= 2) ...
如果textread是輸入'C:\21.txt'就沒有這問題
但fullname不是也是這樣嗎...??
請問我該如何修正呢?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.114.99.210
1F:→ jeffppp:直接cd(path) filename=dir('*.txt') 這樣就行了吧 07/19 17:40
抱歉,可能是我新手的關係
這樣要怎麼連續讀檔案呢??
※ 編輯: xj04a83 來自: 118.169.34.190 (07/22 16:18)
自行解決了 :)
因為用原文中的方法得到的檔案名稱會有單引號 (cell數據類型)
而textread是讀字串,因此先將名稱轉為字元
path = 'C:\';
allfile = dir(path);
filename = char({allfile(~[allfile.isdir]).name}); %轉成字元+忽略讀進的folder
[m,n] = size(filename);
for i = 1:n
file = strcat(path,filename(i,:));
[name number] = textread(file,'%f %f',-1,'headerlines',1);
end
※ 編輯: xj04a83 來自: 118.169.34.190 (07/22 18:10)